python advanced feature iteration

Determine whether an object is an iterable object:

The method is to judge the Iterable type of the collections module

>>> from collectoins import Iterable

>>>isinstance('abc',Iterable) #str is iterable

>>>isinstance([1,2,3],Iterable) #Is the list iterable

>>>isinstance(123,Iterable) #Whether the integer is iterable

 

If you want to implement a JAVA-like subscript loop for LIST, you can use Python's built-in enumerate function to turn a LIST into an index-element pair, so that you can iterate over the index and the element itself in the for loop at the same time.

>>> for i, value in enumerate(['a','b','c']);

>>>        print i, value

Result returned: 0,a 

                   1,b

                   2,c

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325124034&siteId=291194637