My CSDN blog column: https://blog.csdn.net/yty_7
Github address: https://github.com/yot777/
Before reviewing the loop structure, let's talk about the concept of traversal.
The concept of traversal
The so-called traversal (Traversal) refers to following a certain search route, in turn to do each element in the combined structure once and only once .
Traversal of basic composite structure
Of the six data structures in Python, lists, tuples, sets, and dictionaries all belong to the basic combined structure.
You can use the for ... in ... : statement of the Python loop structure to traverse.
The result of the traversal is to "count" all the elements from left to right .
List traversal
>>> s = ['a','b',0,1,'c','d']
>>> for i in s:
... print("i=",i)
运行结果:
i= a
i= b
i= 0
i= 1
i= c
i= d
Note: When traversing each element of the list, the variable in the middle of for ... in need not be defined in advance.
Python can automatically traverse from the element of the list to what type it is, and automatically determine what type the variable is.
Traversal of tuples
>>> a = ('x','x',2,2,'y')
>>> for j in a:
... print("j=",j)
运行结果:
j= x
j= x
j= 2
j= 2
j= y
Similarly, when traversing each element of the tuple, the variable in the middle of for ... in need not be defined in advance.
Collection traversal
>>> s = {'x','x',2,2,'y'}
>>> for k in s:
... print("k=",k)
运行结果:
k= x
k= 2
k= y
Please note that because the elements in the collection cannot be repeated, Python de-duplicates the elements of the collection before traversing.
Dictionary traversal
The situation of the dictionary is slightly different from the above three data types. It can traverse the key, value, and key-value (key-value pair) together.
If you use the most common for ... in ... : statement to traverse, the result of the traversal is the key value
>>> d = {'name':'Tom','age':20,'gender':'male'}
>>> for i in d:
... print(i)
运行结果:
name
age
gender
>>>
When only traversing the key value, adding .keys () after the dictionary name has the same effect
>>> d = {'name':'Tom','age':20,'gender':'male'}
>>> for i in d.keys():
... print(i)
运行结果:
name
age
gender
If you want to traverse the value, you need to add .values () after the dictionary name
>>> d = {'name':'Tom','age':20,'gender':'male'}
>>> for i in d.values():
... print(i)
运行结果:
Tom
20
male
If you want to traverse the key-value pairs together, you need to add .items () after the dictionary name
>>> d = {'name':'Tom','age':20,'gender':'male'}
>>> for s in d.items():
... print(s)
运行结果:
('name', 'Tom')
('age', 20)
('gender', 'male')
to sum up
Traversal is to do each element in the combined structure once and only once .
Use the for ... in ... : statement of the Python loop structure to traverse.
The result of the traversal is to "count" all the elements from left to right .
The variables in the middle of for ... in need not be defined in advance.
After the dictionary name, add .keys () to traverse the dictionary key value.
After the dictionary name, add .values () to traverse the value of the dictionary.
The dictionary name is appended with .items () to traverse the dictionary's key-value (key-value pair).
My CSDN blog column: https://blog.csdn.net/yty_7
Github address: https://github.com/yot777/
If you think this chapter is helpful to you, welcome to follow, comment and like! Github welcomes your Follow and Star!