Iterable trong Python là gì
Nói một cách đơn giản, bất kỳ đối tượng nào có thể được lặp lại đều có thể hiểu được. Chẳng hạn, một đối tượng danh sách là có thể sử dụng được và một đối tượng STR cũng vậy. Các số danh sách và tên chuỗi là Iterables vì chúng tôi có thể lặp lại chúng (sử dụng vòng lặp trong trường hợp này). & nbsp; Trong bài viết này, chúng ta sẽ xem cách kiểm tra xem một đối tượng có thể sử dụng được trong Python không. check if an object is iterable in Python. Show
Nội dung chính Show
Examples:
Ví dụ 1: Python3H e l l o3 H e l l o4 H e l l o5 H e l l o6 TypeError: 'int' object is not iterable0 TypeError: 'int' object is not iterable1 TypeError: 'int' object is not iterable2 TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 TypeError: 'int' object is not iterable5 H e l l o4 TypeError: 'int' object is not iterable7 TypeError: 'int' object is not iterable8 Output: H e l l o Ví dụ 2: Python3TypeError: 'int' object is not iterable9 H e l l o4 TypeError: 'int' object is not iterable01 H e l l o6 TypeError: 'int' object is not iterable03 TypeError: 'int' object is not iterable1 TypeError: 'int' object is not iterable05 TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 TypeError: 'int' object is not iterable08 Đầu ra TypeError: 'int' object is not iterable Tuy nhiên, một đối tượng INT không thể hiểu được và một đối tượng nổi cũng vậy. Số đối tượng số nguyên không phải là điều đáng tin cậy, vì chúng ta không thể lặp lại nó.int object is not iterable and so is a float object. The integer object number is not iterable, as we are not able to loop over it. Kiểm tra khả năng lặp lại của một đối tượng trong PythonChúng ta sẽ khám phá các cách khác nhau để kiểm tra xem một đối tượng có thể có được hay không. Chúng tôi sử dụng hàm HasAttr () để kiểm tra xem tên đối tượng chuỗi có thuộc tính __iter__ để kiểm tra khả năng lặp lại hay không. Tuy nhiên, kiểm tra này không toàn diện.iterable or not. We use the hasattr() function to test whether the string object name has __iter__ attribute for checking iterability. However, this check is not comprehensive. Phương pháp 1: Sử dụng kiểm tra phương thức __iter__.Python3H e l l o3 H e l l o4 TypeError: 'int' object is not iterable01 TypeError: 'int' object is not iterable02 TypeError: 'int' object is not iterable03 TypeError: 'int' object is not iterable04 TypeError: 'int' object is not iterable05 TypeError: 'int' object is not iterable06 TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 TypeError: 'int' object is not iterable09 TypeError: 'int' object is not iterable00 TypeError: 'int' object is not iterable8 TypeError: 'int' object is not iterable02 TypeError: 'int' object is not iterable03 TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 TypeError: 'int' object is not iterable09 TypeError: 'int' object is not iterable07 TypeError: 'int' object is not iterable8 Output: TypeError: 'int' object is not iterable0 Phương pháp 2: Sử dụng lớp ITEBLE.Chúng tôi có thể xác minh rằng một đối tượng có thể sử dụng được bằng cách kiểm tra xem đó có phải là một ví dụ của lớp Iterable hay không. Kiểm tra cá thể báo cáo đối tượng chuỗi là điều chỉnh chính xác bằng cách sử dụng lớp ITBER. Điều này hoạt động cho Python 3 là tốt. Đối với Python 3.3 trở lên, bạn sẽ phải nhập lớp ITerable từ bộ sưu tập.abc chứ không phải từ các bộ sưu tập như vậy:collections.abc and not from collections like so: Python3TypeError: 'int' object is not iterable09 TypeError: 'int' object is not iterable80 TypeError: 'int' object is not iterable81 TypeError: 'int' object is not iterable82 H e l l o3 H e l l o4 TypeError: 'int' object is not iterable01 TypeError: 'int' object is not iterable02 TypeError: 'int' object is not iterable03 TypeError: 'int' object is not iterable04 TypeError: 'int' object is not iterable05 TypeError: 'int' object is not iterable06 TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 TypeError: 'int' object is not iterable09 H e l l o32 TypeError: 'int' object is not iterable8 TypeError: 'int' object is not iterable02 TypeError: 'int' object is not iterable03 TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 TypeError: 'int' object is not iterable09 H e l l o39 TypeError: 'int' object is not iterable8 Output: TypeError: 'int' object is not iterable0 TypeError: 'int' object is not iterable3TypeError: 'int' object is not iterable4Roster is iterable9Roster is iterable0TypeError: 'int' object is not iterable8Phương pháp 2: Sử dụng lớp ITEBLE.iter built-in function works in the following way:
Code: Python3TypeError: 'int' object is not iterable02 TypeError: 'int' object is not iterable87 TypeError: 'int' object is not iterable88 H e l l o44 TypeError: 'int' object is not iterable03 Phương pháp 3: Sử dụng hàm tích hợp iter (). TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 H e l l o51 H e l l o52 H e l l o53 H e l l o54 H e l l o55 Chức năng tích hợp ITER hoạt động theo cách sau: TypeError: 'int' object is not iterable3 TypeError: 'int' object is not iterable4 H e l l o51 H e l l o61 H e l l o53 H e l l o54 H e l l o55 Output: TypeError: 'int' object is not iterable0 Kiểm tra xem đối tượng có thực hiện __iter__ hay không và gọi nó để có được một trình lặp.Nếu điều đó thất bại, Python sẽ tăng loại, thường nói rằng đối tượng C C không thể điều chỉnh được, trong đó C là lớp của đối tượng đích. Python3H e l l o3 H e l l o4 H e l l o43 TypeError: 'int' object is not iterable3 H e l l o477 ____78 TypeError: 'int' object is not iterable04 TypeError: 'int' object is not iterable4 H e l l o51 TypeError: 'int' object is not iterable07 TypeError: 'int' object is not iterable8 TypeError: 'int' object is not iterable02 TypeError: 'int' object is not iterable03 TypeError: 'int' object is not iterable04 TypeError: 'int' object is not iterable4 H e l l o51 TypeError: 'int' object is not iterable14 TypeError: 'int' object is not iterable8 Output: TypeError: 'int' object is not iterable8 Là chuỗi có được không?Ví dụ, chuỗi cũng có thể lặp lại. Nếu một đối tượng về mặt kỹ thuật không phải là một mảng, nhưng đại diện cho một bộ sưu tập (danh sách, bộ) của một cái gì đó, thì vì..ous là một cú pháp tuyệt vời để lặp qua nó, vì vậy hãy xem cách làm cho nó hoạt động.strings are also iterable. If an object isn't technically an array, but represents a collection (list, set) of something, then for..of is a great syntax to loop over it, so let's see how to make it work. Những loại nào đó có thể hiểu được trong Python?Ví dụ về các vòng lặp bao gồm tất cả các loại trình tự (như danh sách, str và tuple) và một số loại không theo trình tự như dict, đối tượng tệp và đối tượng của bất kỳ lớp nào bạn xác định bằng phương thức __iter __ () hoặc bằng phương thức __getItem __ () mà phương thức mà phương thức màthực hiện trình tự ngữ nghĩa. |