Hướng dẫn zip 2 lists python - zip 2 danh sách python

Có rất nhiều trường hợp trong Python nơi cần có liên kết giữa hai hoặc nhiều trình lặp, chẳng hạn như bộ dữ liệu, từ điển, danh sách và bộ. Zipping là thuật ngữ Python để ghép các trình lặp như vậy. Trong bài viết này, làm thế nào Python zip hai danh sách cùng nhau.

Danh sách trong Python giống như các mảng có kích thước động, được khai báo bằng các ngôn ngữ khác (vectơ trong C ++ và ArrayList trong Java). Danh sách không cần phải luôn luôn đồng nhất, điều này làm cho nó trở thành công cụ mạnh mẽ nhất trong Python. Để tìm hiểu thêm về danh sách Python, hãy truy cập bài viết của chúng tôi 3 cách để tìm kích thước danh sách Python.
lists, visit our article “3 Ways to Find Python List Size“.

Sử dụng hàm zip bình thường, bạn có thể dễ dàng tổng hợp nội dung của lớp container. Tuy nhiên, có những lúc, khi nhiều danh sách và danh sách chứa được yêu cầu làm thành phần chỉ mục và bạn phải hợp nhất chúng. Đây là một tình huống kỳ lạ, nhưng giải pháp rất đơn giản. Vì vậy, hãy để Lôi xem mọi thứ hoạt động như thế nào!
let’s see how things work!

Làm thế nào để zip hai danh sách trong Python?

  • Làm thế nào để zip hai danh sách trong Python?
    • 1) Sử dụng hàm zip () tích hợp
    • 2) Sử dụng bản đồ () + __add__
    • 3) Sử dụng ‘cho vòng lặp với chức năng Zip ()
  • Bản tóm tắt

Dưới đây là ba phương pháp mà Python ZIP hai danh sách:

1) Sử dụng hàm zip () tích hợp

2) Sử dụng bản đồ () + __add__zip() function. Using the zip() function, you can create an iterator object containing tuples (know more about tuple at
3 Ways to Convert Lists to Tuple in Python“). It can be helpful to think of the
zip() function as combining two or more lists (or other iterable objects) into an object containing ordered tuples from the lists. The below example shows how the zip() function can easily merge two lists in python without any
extra effort.

3) Sử dụng ‘cho vòng lặp với chức năng Zip ()

list_a = [1, 3, 4]
list_b = [5, 7, 11]

list_zip = zip(list_a, list_b)

zipped_list = list(list_zip)
print(zipped_list)

Output:

[(1, 5), (3, 7), (4, 11)]

2) Sử dụng bản đồ () + __add__

3) Sử dụng ‘cho vòng lặp với chức năng Zip ()in-built python method similar to zip() function above. It enables you to zip the elements of the iterable by mapping the elements of the first iterable with the elements of the second iterable. By using the map() function along with the addition
operator, you can merge two lists in python as shown in the below example:

list_1 = [[2, 3], [4, 5], [7, 6]]
list_2 = [[4, 9], [4, 2], [11, 10]]
  
print ("The given list 1 is : " + str(list_1))
print ("The given list 2 is : " + str(list_2))
  

res = list(map(list.__add__, list_1, list_2))
      
print ("The zipped list is : " +  str(res))

Output:

The given list 1 is : [[2, 3], [4, 5], [7, 6]]
The given list 2 is : [[4, 9], [4, 2], [11, 10]]
The zipped list is : [[2, 3, 4, 9], [4, 5, 4, 2], [7, 6, 11, 10]]

3) Sử dụng ‘cho vòng lặp với chức năng Zip ()

Bản tóm tắt
make use of chain() function which is part of python itertools library and therefore, the library is imported using “import” keyword before beginning the source code. Check out the below example for better understanding of this method.

3) Sử dụng ‘cho vòng lặp với chức năng Zip ()

import itertools
  
list_1 = [[1, 3], [4, 5], [5, 6]]
list_2 = [[11, 9], [16, 2], [4, 10]]
  
print ("The given list 1 is : " + str(list_1))
print ("The given list 2 is : " + str(list_2))
  
res = [list(itertools.chain(*i)) 
       for i in zip(list_1, list_2)]
      
print ("The zipped list is : " +  str(res))

Output:

The given list 1 is : [[1, 3], [4, 5], [5, 6]]
The given list 2 is : [[11, 9], [16, 2], [4, 10]]
The zipped list is : [[1, 3, 11, 9], [4, 5, 16, 2], [5, 6, 4, 10]]

Bản tóm tắt

Dưới đây là ba phương pháp mà Python ZIP hai danh sách:
language and hence face the difficult situation to zip the elements while programming. In this article, we studied various methods to zip the elements of two lists and you can use any of them according to the different situations you face while coding the program. However, the zip() function is highly used and the most recommended method to zip the lists faster and efficiently.