Iterable là một đối tượng, mà người ta có thể lặp đi lặp lại. Nó tạo ra một Iterator khi được chuyển đến phương thức iter[]. Trình vòng lặp là một đối tượng, được sử dụng để lặp qua một đối tượng có thể lặp lại bằng phương thức __next__[]. Trình lặp có phương thức __next__[], trả về mục tiếp theo của đối tượng. Lưu ý rằng mọi iterator cũng là iterable, nhưng không phải iterable nào cũng là iterator. Ví dụ: một danh sách có thể lặp lại nhưng một danh sách không phải là một trình vòng lặp. Một iterator có thể được tạo từ một iterable bằng cách sử dụng hàm iter[]. Để làm được điều này, lớp của một đối tượng cần có phương thức __iter__, phương thức này trả về một trình vòng lặp hoặc phương thức __getitem__ với các chỉ mục tuần tự bắt đầu bằng 0.
Nội dung chính Hiển thị
- Kiểm tra danh sách tích hợp và trình lặp danh sách
- Tách một iterator khỏi một iterable
- Sự khác biệt giữa iterator và danh sách là gì?
- Danh sách Python có phải là trình vòng lặp không?
- Sự khác biệt giữa iterator và iterable trong Python là gì?
- Danh sách có thể lặp lại hoặc trình lặp trong Python không?
Mã số 1
Python3
đầu ra
Traceback [most recent call last]: File "/home/1c9622166e9c268c0d67cd9ba2177142.py", line 2, in next["GFG"] TypeError: 'str' object is not an iterator
Chúng tôi biết rằng str có thể lặp lại nhưng nó không phải là trình lặp. trong đó nếu chúng ta chạy cái này trong vòng lặp for để in chuỗi thì điều đó là có thể bởi vì khi vòng lặp for thực thi, nó sẽ chuyển đổi thành một trình vòng lặp để thực thi mã.
Python3
Ở đây iter[ ] đang chuyển đổi s là một chuỗi [có thể lặp lại] thành một iterator và in G lần đầu tiên chúng ta có thể gọi nhiều lần để lặp qua các chuỗi
Khi một vòng lặp for được thực thi, câu lệnh for gọi iter[] trên đối tượng mà nó được cho là sẽ lặp lại. Nếu cuộc gọi này thành công, cuộc gọi iter sẽ trả về một đối tượng iterator xác định phương thức __next__[], truy cập lần lượt từng phần tử của đối tượng. Phương thức __next__[] sẽ đưa ra một ngoại lệ StopIteration nếu không còn phần tử nào khả dụng. Vòng lặp for sẽ kết thúc ngay khi nó bắt gặp ngoại lệ StopIteration. Hãy gọi phương thức __next__[] bằng hàm tích hợp next[].
Mã số 2. Chức năng 'có thể lặp lại' sẽ trả về True nếu đối tượng 'obj' là có thể lặp lại và Sai nếu không.
Python3
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False0
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False1
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False2
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False3
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False1
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False5
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False6
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False7____18____19
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False10
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False7____18____19
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False10
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False7____18____19
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False10
đầu ra
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False0
Ghi chú. Nếu ‘next[iterator_obj]’ được gọi thêm một lần nữa, nó sẽ trả về ‘StopIteration’.
Mã số 3. Kiểm tra đối tượng có thể lặp lại hay không
đầu ra
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
Bản tóm tắt. trong hướng dẫn này, bạn sẽ tìm hiểu về Python iterator và iterable và sự khác biệt của chúng
vòng lặp
Iterator là một đối tượng thực hiện giao thức iterator. Nói cách khác, iterator là một đối tượng thực hiện các phương thức sau
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
19 trả về chính đối tượng iterator34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
30 trả về phần tử tiếp theo
Sau khi bạn hoàn thành việc lặp lại một bộ sưu tập bằng trình lặp, trình lặp sẽ cạn kiệt. Điều đó có nghĩa là bạn không thể sử dụng đối tượng iterator nữa
Iterables
Một iterable là một đối tượng mà bạn có thể lặp đi lặp lại
Một đối tượng có thể lặp lại khi nó triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19. Và phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 của nó trả về một iterator mới
Kiểm tra danh sách tích hợp và trình lặp danh sách
Trong Python, danh sách là tập hợp các mục được sắp xếp theo thứ tự. Nó cũng là một iterable vì một đối tượng danh sách có phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 trả về một iterator. Ví dụ
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False1
đầu ra
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False3
Trong ví dụ này, phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 trả về một iterator có kiểu
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False35
Bởi vì
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False35 triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19, bạn có thể sử dụng hàm tích hợp sẵn
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False5 để lấy đối tượng iterator
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False4
Vì
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False35 cũng triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False30, nên bạn có thể sử dụng hàm tích hợp sẵn
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False9 để lặp lại danh sách
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False8
Nếu bạn gọi hàm
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False9 một lần nữa, bạn sẽ nhận được một ngoại lệ
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False43
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False1
Lỗi
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False2
Điều này là do trình lặp danh sách đã hết. Để lặp lại danh sách, bạn cần tạo một trình lặp mới
Điều này minh họa việc tách danh sách khỏi trình vòng lặp của nó. Danh sách được tạo một lần trong khi trình vòng lặp được tạo mỗi khi bạn cần lặp lại danh sách
Sau đây định nghĩa lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False4
Trong ví dụ này, lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 đóng hai vai trò. iterable và iterator
Lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 là một iterator vì nó triển khai cả phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 và
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False30. Phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 trả về chính đối tượng đó. Và phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False30 trả về mục tiếp theo từ danh sách
Lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 cũng là một iterable vì nó triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 trả về chính một đối tượng, là một iterator
Phần sau đây tạo một thể hiện mới của lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 và lặp lại các phần tử của nó bằng vòng lặp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False84
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False00
Khi bạn hoàn thành việc lặp lại, đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False85 trở nên vô dụng. Nếu bạn cố lặp lại lần nữa, bạn sẽ nhận được một ngoại lệ
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False43
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False01
Lỗi
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False2
Nếu bạn sử dụng vòng lặp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False84, bạn sẽ không nhận lại được gì. Trình vòng lặp trống
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False03
Để lặp lại, bạn cần tạo một đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False85 mới với thuộc tính
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False89. Điều này là không hiệu quả
Tách một iterator khỏi một iterable
Hãy tách color iterator khỏi iterable của nó giống như những gì Python làm với list iterator và list
Sau đây định nghĩa lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False04
Sau đây định nghĩa lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False05
Làm thế nào nó hoạt động
- Phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
12 chấp nhận một iterable là một thể hiện của lớp34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
44 - Phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
19 trả về chính iterator - Phương thức __next__ trả về phần tử tiếp theo từ đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
44
Phần sau đây cho thấy cách sử dụng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11 để lặp lại đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False06
Để lặp lại đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44, bạn chỉ cần tạo một phiên bản mới của
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11
Có một vấn đề
Khi bạn muốn lặp lại đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44, bạn cần tự tạo một đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11 mới. Và bạn cũng cần nhớ tên iterator
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11
Sẽ thật tuyệt nếu bạn có thể tự động hóa việc này. Để làm điều đó, bạn có thể làm cho lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 có thể lặp lại bằng cách triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False07
Phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 trả về một thể hiện mới của lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11
Bây giờ, bạn có thể lặp lại đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 mà không cần tạo đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11 một cách rõ ràng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False08
Bên trong, vòng lặp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False84 gọi phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False19 của đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False85 để lấy bộ lặp và sử dụng bộ lặp này để lặp qua các phần tử của đối tượng
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False85
Sau đây đặt lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False11 bên trong lớp
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False44 để đóng gói chúng thành một lớp duy nhất
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False09
Bản tóm tắt
- Một iterable là một đối tượng triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
19 trả về một iterator - Trình vòng lặp là một đối tượng triển khai phương thức
34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
19 trả về chính nó và phương thức34 is iterable : False [4, 5] is iterable : True [4, 5] is iterable : True {'a': 4} is iterable : True dfsdf is iterable : True 4.5 is iterable : False
30 trả về phần tử tiếp theo - Iterators cũng là iterables
Bạn có thấy hướng dẫn này hữu ích không?
Sự khác biệt giữa iterator và danh sách là gì?
Iterator chỉ có thể di chuyển theo hướng thuận trong khi ListIterator di chuyển theo cả hướng tiến và lùi