In the process of development, if you are given a list or a tuple, we can traverse the list or tuple through a for loop, which iterates over us as an iteration (iteration). In Python, iterations are done through for ... in
, and in many languages such as C or Java, iteration lists are done by subscript, such as Java code
: 1 for (int x = 0;x<n;x++) 2 { 3 a[i] = I 4 }
As you can see, Python is for
more abstract than the Java for
Loop because the Python for
loop can be used not only on a list or tuple, but also on other iterative objects. List this data type although there is subscript, but many other data types are not subscript, but as long as the object can be iterated, whether or not subscript, can iterate, such as dict can also iterate.
1. Iteration of the list:
1 >>> list1 = []2 >>> n =3 for in range (n) :4... List1.append (i)56 >>> list17 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]8
2. Iterations of tuple tuples:
3. Iteration of the Dictionary (Dict):
1>>>#Iteration of the dictionary2>>> Dict1 = {'a': 1,'b': 2,'C': 3,'D': 4}3>>>Dict14{'a': 1,'C': 3,'b': 2,'D': 4}5>>> forKeyinchDict1:6...PrintKey7 ... 8 a9 CTen b One D A>>>
Because Dict storage is not ordered in list order, the resulting order of the iterations is likely to be different. By default, the Dict iteration is key. If you want to iterate over value, for value in d.itervalues()
You can use it if you want to iterate both key and for k, v in d.iteritems()
valueat the same time.
1 def Get_tp_type_name (value): 2 if is None: 3 return "" 4 5 for inch Tp_type_choice: 6 if k = = Value:7 return V
4. Iterations of arbitrary strings:
1>>> forChinch "ABCADADADWF":2...PrintCH3 ... 4 a5 b6 C7 a8 D9 aTen D One a A D - W - F the>>>
So, when we use for
a loop, the loop works as long as it works on an iterative object, for
and we don't care much about whether the object is a list or another data type. How can I tell if an object is an iterative object? The method is judged by the iterable type of the collections module:
1>>> fromCollectionsImportiterable2>>> Isinstance ('Abudauidua', iterable)#whether Str can iterate3 True4>>> isinstance (dict1,iterable)#whether Dict1 can iterate5 True6>>> isinstance (list1,iterable)#whether List1 can iterate7 True8>>> isinstance (tuple1,iterable)#whether List1 can iterate9 TrueTen>>> isinstance (123456789,iterable)#全数字Whether you can iterate One False A>>>
What if you want to implement subscript loops like Java for a list? Python's enumerate
built-in functions can turn a list into an index-element pair so that the for
index and the element itself can be iterated at the same time in the loop:
1>>> forI, ValueinchEnumerate (['a','b','C','D']):2...PrintI, Value3 ... 4 0 A51b62C73D8>>>
It's a common thing in python to refer to two variables at a time in one iteration, so it shouldn't be too strange, for example: At first I was surprised to see such a loop output statement, and I got used to it slowly.
1 for inch [(3,4), (5,6)]: 2 ... Print x, y 3 4 1 25 3 46 5 6
Python learning "Iteration from beginner to Mastery"