Hướng dẫn python zip every other element - python zip mọi phần tử khác

Tôi cảm thấy như mình dành nhiều thời gian để viết mã bằng Python, nhưng không đủ thời gian để tạo mã Pythonic. Gần đây tôi gặp phải một vấn đề nhỏ vui nhộn mà tôi nghĩ có thể có một giải pháp dễ dàng, thành ngữ. Paraphrasing bản gốc, tôi cần thu thập mọi cặp tuần tự trong một danh sách. Ví dụ: với danh sách

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
6, tôi muốn tính toán
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
7.

Show

Tôi đã đưa ra một giải pháp nhanh chóng vào thời điểm đó trông giống như Java được dịch. Xem lại câu hỏi, điều tốt nhất tôi có thể làm là

l = [1,2,3,4,5,6]
[(l[2*x],l[2*x+1]) for x in range(len(l)/2)]

trong đó có tác dụng phụ của việc tung ra số cuối cùng trong trường hợp độ dài không phải.

Có một cách tiếp cận thành ngữ hơn mà tôi đang thiếu, hay đây là cách tốt nhất tôi sẽ nhận được?

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Lặp lại song song với chức năng Zip () của Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Parallel Iteration With Python's zip() Function

Chức năng Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 tạo ra một trình lặp sẽ tổng hợp các phần tử từ hai hoặc nhiều vòng lặp. Bạn có thể sử dụng trình lặp kết quả để giải quyết nhanh chóng và nhất quán các vấn đề lập trình phổ biến, như tạo từ điển. Trong hướng dẫn này, bạn sẽ khám phá ra logic đằng sau hàm Python
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 và cách bạn có thể sử dụng nó để giải quyết các vấn đề trong thế giới thực.

Đến cuối hướng dẫn này, bạn sẽ học:

  • Cách
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8 hoạt động trong cả Python 3 và Python 2
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8
    works in both Python 3 and Python 2
  • Cách sử dụng hàm Python
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8 để lặp song songparallel iteration
  • Cách tạo từ điển khi đang bằng cách sử dụng
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8create dictionaries on the fly using
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8

Hiểu chức năng Python >>> numbers = [1, 2, 3] >>> letters = ['a', 'b', 'c'] >>> zipped = zip(numbers, letters) >>> zipped # Holds an iterator object <zip object at 0x7fa4831153c8> >>> type(zipped) <class 'zip'> >>> list(zipped) [(1, 'a'), (2, 'b'), (3, 'c')] 8

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 có sẵn trong không gian tên tích hợp. Nếu bạn sử dụng
>>> s1 = {2, 3, 1}
>>> s2 = {'b', 'a', 'c'}
>>> list(zip(s1, s2))
[(1, 'a'), (2, 'c'), (3, 'b')]
5 để kiểm tra
>>> s1 = {2, 3, 1}
>>> s2 = {'b', 'a', 'c'}
>>> list(zip(s1, s2))
[(1, 'a'), (2, 'c'), (3, 'b')]
6, thì bạn sẽ thấy
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 ở cuối danh sách:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']

Bạn có thể thấy rằng

>>> s1 = {2, 3, 1}
>>> s2 = {'b', 'a', 'c'}
>>> list(zip(s1, s2))
[(1, 'a'), (2, 'c'), (3, 'b')]
8 là mục cuối cùng trong danh sách các đối tượng có sẵn.

Theo tài liệu chính thức, chức năng Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 hoạt động như sau:

Trả về một trình lặp của các bộ dữ liệu, trong đó bộ thứ ba chứa phần tử thứ i từ mỗi chuỗi đối số hoặc các vòng lặp. Trình lặp dừng khi có thể cạn kiệt đầu vào ngắn nhất. Với một đối số có thể lặp lại duy nhất, nó trả về một trình lặp gồm 1 bộ. Không có đối số, nó trả về một trình lặp trống. (Nguồn)

Bạn sẽ giải nén định nghĩa này trong suốt phần còn lại của hướng dẫn. Khi bạn làm việc thông qua các ví dụ mã, bạn sẽ thấy rằng các hoạt động của Python Zip hoạt động giống như dây kéo vật lý trên túi hoặc đôi quần jean. Các cặp răng xen kẽ ở cả hai mặt của dây kéo được kéo lại với nhau để đóng một lỗ mở. Trên thực tế, sự tương tự trực quan này là hoàn hảo để hiểu

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8, vì hàm được đặt tên theo khóa kéo vật lý!

Sử dụng >>> numbers = [1, 2, 3] >>> letters = ['a', 'b', 'c'] >>> zipped = zip(numbers, letters) >>> zipped # Holds an iterator object <zip object at 0x7fa4831153c8> >>> type(zipped) <class 'zip'> >>> list(zipped) [(1, 'a'), (2, 'b'), (3, 'c')] 8 trong Python

Hàm Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 được định nghĩa là
>>> zipped = zip()
>>> zipped
<zip object at 0x7f196294a488>
>>> list(zipped)
[]
3. Chức năng lấy Iterables làm đối số và trả về trình lặp. Iterator này tạo ra một loạt các bộ dữ liệu chứa các yếu tố từ mỗi phần có thể.
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 có thể chấp nhận bất kỳ loại khác nhau, chẳng hạn như tệp, danh sách, bộ dữ liệu, từ điển, bộ, v.v.iterator. This iterator generates a series of tuples containing elements from each iterable.
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 can accept any type of iterable, such as files, lists, tuples, dictionaries, sets, and so on.

Vượt qua >>> zipped = zip() >>> zipped <zip object at 0x7f196294a488> >>> list(zipped) [] 5 đối số

Nếu bạn sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 với các đối số
>>> zipped = zip()
>>> zipped
<zip object at 0x7f196294a488>
>>> list(zipped)
[]
5, thì hàm sẽ trả về một trình lặp tạo ra các bộ dữ liệu có độ dài
>>> zipped = zip()
>>> zipped
<zip object at 0x7f196294a488>
>>> list(zipped)
[]
5. Để xem điều này trong hành động, hãy xem khối mã sau:

>>>

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]

Ở đây, bạn sử dụng

>>> zipped = zip()
>>> zipped
<zip object at 0x7f196294a488>
>>> list(zipped)
[]
9 để tạo một trình lặp lại tạo ra các bộ dữ liệu của mẫu
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
0. Trong trường hợp này, các giá trị
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
1 được lấy từ
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và các giá trị
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
3 được lấy từ
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4. Lưu ý cách hàm Python
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trả về một trình lặp. Để truy xuất đối tượng danh sách cuối cùng, bạn cần sử dụng
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
6 để tiêu thụ trình lặp.

Nếu bạn làm việc với các chuỗi như danh sách, bộ dữ liệu hoặc chuỗi, thì các phép lặp của bạn được đảm bảo sẽ được đánh giá từ trái sang phải. Điều này có nghĩa là danh sách kết quả của các bộ dữ liệu sẽ có dạng

>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
7. Tuy nhiên, đối với các loại lặp khác (như bộ), bạn có thể thấy một số kết quả kỳ lạ:

>>>

>>> s1 = {2, 3, 1}
>>> s2 = {'b', 'a', 'c'}
>>> list(zip(s1, s2))
[(1, 'a'), (2, 'c'), (3, 'b')]

Trong ví dụ này,

>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
8 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
9 là các đối tượng
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
0, mà don don giữ các yếu tố của chúng theo bất kỳ thứ tự cụ thể nào. Điều này có nghĩa là các bộ dữ liệu được trả về bởi
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 sẽ có các phần tử được ghép nối ngẫu nhiên. Nếu bạn sẽ sử dụng chức năng Python
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 với các vòng lặp không có thứ tự như các bộ, thì đây là điều cần lưu ý.

Không có tranh luận

Bạn có thể gọi

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 không có đối số. Trong trường hợp này, bạn chỉ cần nhận được một trình lặp trống:

>>>

>>> zipped = zip()
>>> zipped
<zip object at 0x7f196294a488>
>>> list(zipped)
[]

Ở đây, bạn gọi

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 không có đối số, vì vậy biến
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
5 của bạn chứa một trình lặp trống. Nếu bạn tiêu thụ trình lặp với
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
6, thì bạn cũng sẽ thấy một danh sách trống.

Bạn cũng có thể cố gắng buộc người lặp trống để mang lại một phần tử trực tiếp. Trong trường hợp này, bạn sẽ nhận được ngoại lệ

>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
7:

>>>

>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration

Khi bạn gọi

>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
8 vào
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
5, Python cố gắng lấy lại mục tiếp theo. Tuy nhiên, kể từ khi
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
5 giữ một máy lặp trống, không có gì để rút ra, vì vậy Python làm tăng một ngoại lệ
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
7.

Vượt qua một lập luận

Chức năng Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cũng có thể chỉ có một đối số. Kết quả sẽ là một trình lặp lại mang lại một loạt các bộ đếm 1 mục:

>>>

>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]

Điều này có thể không hữu ích, nhưng nó vẫn hoạt động. Có lẽ bạn có thể tìm thấy một số trường hợp sử dụng cho hành vi này của

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8!

Như bạn có thể thấy, bạn có thể gọi chức năng Python

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 với nhiều lần lặp lại mà bạn cần. Độ dài của các bộ dữ liệu kết quả sẽ luôn bằng số lần lặp bạn vượt qua dưới dạng đối số. Ở đây, một ví dụ với ba lần lặp:

>>>

>>> integers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> floats = [4.0, 5.0, 6.0]
>>> zipped = zip(integers, letters, floats)  # Three input iterables
>>> list(zipped)
[(1, 'a', 4.0), (2, 'b', 5.0), (3, 'c', 6.0)]

Ở đây, bạn gọi hàm Python

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 với ba lần lặp, do đó, các bộ dữ liệu kết quả có ba phần tử.

Vượt qua các đối số về chiều dài không đồng đều

Khi bạn làm việc với chức năng Python

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8, điều quan trọng là phải chú ý đến độ dài của vòng lặp của bạn. Nó có thể là các vòng lặp mà bạn truyền đi khi các đối số không có cùng chiều dài.

Trong những trường hợp này, số lượng các yếu tố mà

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 đưa ra sẽ bằng với độ dài của số ít nhất có thể. Các yếu tố còn lại trong bất kỳ lần lặp lại nào nữa sẽ bị bỏ qua hoàn toàn bởi
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8, như bạn có thể thấy ở đây:

>>>

>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]

>>> integers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> floats = [4.0, 5.0, 6.0]
>>> zipped = zip(integers, letters, floats)  # Three input iterables
>>> list(zipped)
[(1, 'a', 4.0), (2, 'b', 5.0), (3, 'c', 6.0)]
9 là độ dài của đối tượng đầu tiên (và ngắn nhất)
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
0,
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 đưa ra danh sách năm bộ dữ liệu. Vẫn còn 95 phần tử chưa từng có từ đối tượng
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
0 thứ hai. Tất cả đều bị bỏ qua bởi
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 vì không có thêm các phần tử nào từ đối tượng
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
0 đầu tiên để hoàn thành các cặp.

Nếu các giá trị kéo dài hoặc chưa từng có là quan trọng đối với bạn, thì bạn có thể sử dụng

>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
5 thay vì
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8. Với chức năng này, các giá trị bị thiếu sẽ được thay thế bằng bất cứ điều gì bạn chuyển sang đối số
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
7 (mặc định là
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
8). Việc lặp lại sẽ tiếp tục cho đến khi có thể cạn kiệt lâu nhất:

>>>

>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]

Ở đây, bạn sử dụng

>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
5 để mang lại năm bộ dữ liệu với các phần tử từ
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4,
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
2. Việc lặp lại chỉ dừng lại khi
>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
2 cạn kiệt. Các yếu tố bị thiếu từ
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4 chứa đầy dấu hỏi
>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
6, đó là những gì bạn đã chỉ định với
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
7.

Kể từ Python 3.10,

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 có một đối số từ khóa tùy chọn mới gọi là
>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
9, được giới thiệu thông qua PEP 618, ADD ADD kiểm tra độ dài theo chiều dài để zip. Mục tiêu chính của đối số này là cung cấp một cách an toàn để xử lý các vòng lặp có độ dài không đồng đều.safe way to handle iterables of unequal length.

Giá trị mặc định của

>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
9 là
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
01, đảm bảo rằng
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 vẫn tương thích ngược và có hành vi mặc định phù hợp với hành vi của nó trong các phiên bản Python 3 cũ hơn:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
0

Trong Python> = 3.10, gọi

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 mà không thay đổi giá trị mặc định thành
>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
9 vẫn cung cấp cho bạn một danh sách năm bộ dữ liệu, với các phần tử chưa từng có từ đối tượng
>>> list(zip(range(5), range(100)))
[(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)]
0 thứ hai bị bỏ qua.

Ngoài ra, nếu bạn đặt

>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
9 thành
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
07, thì
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 kiểm tra xem các lần lặp đầu vào mà bạn cung cấp dưới dạng đối số có cùng độ dài, tăng
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
09 nếu chúng không có:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
1

Tính năng mới này của

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 rất hữu ích khi bạn cần đảm bảo rằng chức năng chỉ chấp nhận các vòng lặp có độ dài bằng nhau. Cài đặt
>>> from itertools import zip_longest
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> longest = range(5)
>>> zipped = zip_longest(numbers, letters, longest, fillvalue='?')
>>> list(zipped)
[(1, 'a', 0), (2, 'b', 1), (3, 'c', 2), ('?', '?', 3), ('?', '?', 4)]
9 thành
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
07 Làm cho mã mong đợi các vòng lặp có độ dài bằng nhau an toàn hơn, đảm bảo rằng các thay đổi bị lỗi đối với mã người gọi don lồng dẫn đến việc âm thầm mất dữ liệu.

So sánh >>> numbers = [1, 2, 3] >>> letters = ['a', 'b', 'c'] >>> zipped = zip(numbers, letters) >>> zipped # Holds an iterator object <zip object at 0x7fa4831153c8> >>> type(zipped) <class 'zip'> >>> list(zipped) [(1, 'a'), (2, 'b'), (3, 'c')] 8 trong Python 3 và 2

Chức năng Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 hoạt động khác nhau trong cả hai phiên bản của ngôn ngữ. Trong Python 2,
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trả về một
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 của các bộ dữ liệu. Kết quả
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 bị cắt theo chiều dài của đầu vào ngắn nhất có thể. Nếu bạn gọi
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 không có đối số, thì bạn sẽ nhận được một
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 trống:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
2

Trong trường hợp này, cuộc gọi của bạn đến chức năng Python

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trả về một danh sách các bộ dữ liệu bị cắt ngắn ở giá trị
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
21. Khi bạn gọi
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 không có đối số, bạn sẽ nhận được một
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 trống.

Tuy nhiên, trong Python 3,

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trả về một trình lặp. Đối tượng này mang lại bộ dữ liệu theo yêu cầu và chỉ có thể đi qua một lần. Việc lặp lại kết thúc với một ngoại lệ
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
7 một khi đầu vào ngắn nhất có thể cạn kiệt. Nếu bạn không cung cấp đối số cho
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8, thì hàm sẽ trả về một trình lặp trống:iterator. This object yields tuples on demand and can be traversed only once. The iteration ends with a
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
7 exception once the shortest input iterable is exhausted. If you supply no arguments to
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8, then the function returns an empty iterator:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
3

Tại đây, cuộc gọi của bạn đến

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trả về một trình lặp lại. Lần lặp đầu tiên được cắt ngắn tại
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
21 và lần thứ hai dẫn đến ngoại lệ
>>> a = [1, 2, 3]
>>> zipped = zip(a)
>>> list(zipped)
[(1,), (2,), (3,)]
7. Trong Python 3, bạn cũng có thể mô phỏng hành vi Python 2 của
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 bằng cách gói trình lặp lại được trả về trong một cuộc gọi đến
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
6. Điều này sẽ chạy qua trình lặp và trả lại một danh sách các bộ dữ liệu.

Nếu bạn thường xuyên sử dụng Python 2, thì lưu ý rằng sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 với các vòng lặp đầu vào dài có thể vô tình tiêu thụ nhiều bộ nhớ. Trong những tình huống này, hãy xem xét sử dụng
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
33 thay thế. Hàm này tạo ra một trình lặp tập hợp các phần tử từ mỗi lần lặp. Nó tạo ra hiệu ứng tương tự như
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trong Python 3:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
4

Trong ví dụ này, bạn gọi

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
35 để tạo trình lặp. Khi bạn tiêu thụ bộ lặp được trả về với
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
6, bạn sẽ nhận được một danh sách các bộ dữ liệu, giống như khi bạn đang sử dụng
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 trong Python 3. Lặp lại dừng khi có thể rút được đầu vào ngắn nhất.

Nếu bạn thực sự cần viết mã hành xử theo cùng một cách trong cả Python 2 và Python 3, thì bạn có thể sử dụng một mẹo như sau:

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
5

Ở đây, nếu

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
38 có sẵn trong
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
39, thì bạn sẽ biết rằng bạn sẽ ở Python 2 và
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
38 sẽ được nhập bằng bí danh
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
41. Nếu không, chương trình của bạn sẽ tăng
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
42 và bạn sẽ biết rằng bạn đã ở Python 3. (Tuyên bố
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
43 ở đây chỉ là một trình giữ chỗ.)

Với thủ thuật này, bạn có thể sử dụng chức năng Python

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 một cách an toàn trong suốt mã của mình. Khi chạy, chương trình của bạn sẽ tự động chọn và sử dụng phiên bản chính xác.

Cho đến nay, bạn đã đề cập đến cách chức năng Python từ ____28 hoạt động và tìm hiểu về một số tính năng quan trọng nhất của nó. Bây giờ, thời gian để cuộn tay áo lên và bắt đầu mã hóa các ví dụ trong thế giới thực!

Vòng lặp qua nhiều vòng lặp

Vòng lặp qua nhiều lần lặp là một trong những trường hợp sử dụng phổ biến nhất cho chức năng Python từ ____28. Nếu bạn cần lặp lại thông qua nhiều danh sách, bộ dữ liệu hoặc bất kỳ chuỗi nào khác, thì nó có khả năng là bạn sẽ quay trở lại trên

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8. Phần này sẽ chỉ cho bạn cách sử dụng
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 để lặp qua nhiều lần lặp cùng một lúc.

Danh sách đi ngang song song

Chức năng Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cho phép bạn lặp lại song song hơn hai hoặc nhiều lần lặp. Vì
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 tạo ra các bộ dữ liệu, bạn có thể giải nén chúng trong tiêu đề của vòng lặp
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
51:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
6

Ở đây, bạn lặp lại thông qua loạt các bộ dữ liệu được trả về bởi

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 và giải nén các yếu tố thành
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
53 và
>>> zipped = zip()
>>> zipped
<zip object at 0x7f196294a488>
>>> list(zipped)
[]
5. Khi bạn kết hợp các vòng lặp
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8,
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
51 và giải nén, bạn có thể nhận được một thành ngữ hữu ích và pythonic để đi qua hai hoặc nhiều lần lặp cùng một lúc.

Bạn cũng có thể lặp qua nhiều hơn hai lần lặp trong một vòng

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
51. Xem xét ví dụ sau, có ba lần lặp đầu vào:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
7

Trong ví dụ này, bạn sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 với ba lần lặp để tạo và trả về một trình lặp tạo ra các bộ dữ liệu 3 mục. Điều này cho phép bạn lặp lại thông qua cả ba lần lặp trong một lần. Không có hạn chế nào về số lượng lặp mà bạn có thể sử dụng với chức năng Python từ ____28.

Đi ngang qua từ điển song song

Trong Python 3.6 và hơn thế nữa, từ điển được đặt hàng các bộ sưu tập, có nghĩa là chúng giữ các yếu tố của chúng theo cùng một thứ tự mà chúng được giới thiệu. Nếu bạn tận dụng tính năng này, thì bạn có thể sử dụng chức năng Python

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 để lặp qua nhiều từ điển theo cách an toàn và mạch lạc:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
8

Ở đây, bạn lặp lại thông qua

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
61 và
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
62 song song. Trong trường hợp này,
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 tạo ra các bộ dữ liệu với các mục từ cả hai từ điển. Sau đó, bạn có thể giải nén từng tuple và có quyền truy cập vào các mục của cả hai từ điển cùng một lúc.

Lưu ý rằng, trong ví dụ trên, thứ tự đánh giá từ trái sang phải được đảm bảo. Bạn cũng có thể sử dụng chức năng Python từ

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 để lặp lại thông qua các bộ song song. Tuy nhiên, bạn sẽ cần phải xem xét điều đó, không giống như từ điển trong Python 3.6, các bộ don don giữ các yếu tố của chúng theo thứ tự. Nếu bạn quên chi tiết này, kết quả cuối cùng của chương trình của bạn có thể không hoàn toàn như bạn muốn hoặc mong đợi.

Giải nén một chuỗi

Có một câu hỏi xuất hiện thường xuyên trong các diễn đàn cho Pythonistas mới: Nếu có chức năng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8, vậy tại sao không có chức năng
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
66 mà ngược lại?

Lý do tại sao không có chức năng

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
66 trong Python là vì điều ngược lại với
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 là rất tốt,
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8. Bạn có nhớ rằng hàm Python
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 hoạt động giống như một dây kéo thực sự không? Các ví dụ cho đến nay đã cho bạn thấy Python mọi thứ đã đóng lại như thế nào. Vì vậy, làm thế nào để bạn giải nén các đối tượng Python?

Giả sử bạn có một danh sách các bộ dữ liệu và muốn phân tách các yếu tố của mỗi tuple thành các chuỗi độc lập. Để làm điều này, bạn có thể sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cùng với toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72, như vậy:

>>>

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
9

Ở đây, bạn có một

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

Sắp xếp song song

Sắp xếp là một hoạt động phổ biến trong lập trình. Giả sử bạn muốn kết hợp hai danh sách và sắp xếp chúng cùng một lúc. Để làm điều này, bạn có thể sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cùng với
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
78 như sau:

>>>

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
0

Ở đây, bạn có một

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

Sắp xếp song song

>>>

Ở đây, bạn có một
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

Sắp xếp song song

Sắp xếp là một hoạt động phổ biến trong lập trình. Giả sử bạn muốn kết hợp hai danh sách và sắp xếp chúng cùng một lúc. Để làm điều này, bạn có thể sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cùng với
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
78 như sau:

Trong ví dụ này, trước tiên bạn kết hợp hai danh sách với >>> numbers = [1, 2, 3] >>> letters = ['a', 'b', 'c'] >>> zipped = zip(numbers, letters) >>> zipped # Holds an iterator object <zip object at 0x7fa4831153c8> >>> type(zipped) <class 'zip'> >>> list(zipped) [(1, 'a'), (2, 'b'), (3, 'c')] 8 và sắp xếp chúng. Lưu ý cách >>> dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip'] 80 được sắp xếp bởi >>> zipped = zip() >>> next(zipped) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration 4 và >>> dir(__builtins__) ['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip'] 82 được sắp xếp bởi >>> zipped = zip() >>> next(zipped) Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration 2.

Bạn cũng có thể sử dụng

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
84 và
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cùng nhau để đạt được kết quả tương tự:

Element/Month
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
1
Trong trường hợp này,
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
84 chạy qua trình lặp được tạo bởi
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 và sắp xếp các mục theo
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4, tất cả trong một lần. Cách tiếp cận này có thể nhanh hơn một chút vì bạn chỉ cần hai cuộc gọi chức năng:
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 và
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
84.
Với
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
84, bạn cũng viết một đoạn mã tổng quát hơn. Điều này sẽ cho phép bạn sắp xếp bất kỳ loại trình tự, không chỉ danh sách.
Tính toán theo cặp52,000.00 51,000.00 48,000.00
Bạn có thể sử dụng chức năng Python
>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 để thực hiện một số tính toán nhanh. Giả sử bạn có dữ liệu sau trong bảng tính:
46,800.00 45,900.00 43,200.00

tháng Giêng

>>>

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
2

Ở đây, bạn có một

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

Sắp xếp song song

Sắp xếp là một hoạt động phổ biến trong lập trình. Giả sử bạn muốn kết hợp hai danh sách và sắp xếp chúng cùng một lúc. Để làm điều này, bạn có thể sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cùng với
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
78 như sau:

>>>

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
3

Ở đây, bạn có một

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

>>>

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
4

Ở đây, bạn có một

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

Sắp xếp song song

>>>

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
5

Ở đây, bạn có một

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
16 các bộ dữ liệu chứa một số loại dữ liệu hỗn hợp. Sau đó, bạn sử dụng toán tử giải nén
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
72 để giải nén dữ liệu, tạo hai danh sách khác nhau (
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2 và
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4).

Sắp xếp song song

Sắp xếp là một hoạt động phổ biến trong lập trình. Giả sử bạn muốn kết hợp hai danh sách và sắp xếp chúng cùng một lúc. Để làm điều này, bạn có thể sử dụng

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 cùng với
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
78 như sau:

Trong ví dụ này, trước tiên bạn kết hợp hai danh sách với

>>> numbers = [1, 2, 3]
>>> letters = ['a', 'b', 'c']
>>> zipped = zip(numbers, letters)
>>> zipped  # Holds an iterator object
<zip object at 0x7fa4831153c8>
>>> type(zipped)
<class 'zip'>
>>> list(zipped)
[(1, 'a'), (2, 'b'), (3, 'c')]
8 và sắp xếp chúng. Lưu ý cách
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
80 được sắp xếp bởi
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
4 và
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
82 được sắp xếp bởi
>>> zipped = zip()
>>> next(zipped)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration
2.

  • Bạn cũng có thể sử dụng
    >>> dir(__builtins__)
    ['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
    
    84 và
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8 cùng nhau để đạt được kết quả tương tự:
    in both Python 3 and Python 2
  • >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    1
    and perform different actions on their items in parallel
  • Trong trường hợp này,
    >>> dir(__builtins__)
    ['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
    
    84 chạy qua trình lặp được tạo bởi
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8 và sắp xếp các mục theo
    >>> zipped = zip()
    >>> next(zipped)
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    StopIteration
    
    4, tất cả trong một lần. Cách tiếp cận này có thể nhanh hơn một chút vì bạn chỉ cần hai cuộc gọi chức năng:
    >>> numbers = [1, 2, 3]
    >>> letters = ['a', 'b', 'c']
    >>> zipped = zip(numbers, letters)
    >>> zipped  # Holds an iterator object
    <zip object at 0x7fa4831153c8>
    >>> type(zipped)
    <class 'zip'>
    >>> list(zipped)
    [(1, 'a'), (2, 'b'), (3, 'c')]
    
    8 và
    >>> dir(__builtins__)
    ['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
    
    84.
    on the fly by zipping two input iterables together

Với

>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError', ..., 'zip']
84, bạn cũng viết một đoạn mã tổng quát hơn. Điều này sẽ cho phép bạn sắp xếp bất kỳ loại trình tự, không chỉ danh sách.

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự.Xem nó cùng với hướng dẫn bằng văn bản để hiểu sâu hơn về sự hiểu biết của bạn: Lặp lại song song với chức năng Zip () của Python This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Parallel Iteration With Python's zip() Function