Python uses the item () method to traverse the dictionary.
There are several methods to traverse the Python dictionary. One of them is for... in, which I will not explain. It is almost everywhere in Python. The Traversal method described below is the item () method.
Item ()
The item () method combines every pair of key and value in the dictionary into a tuple, and puts these tuples in the list to return.
DEMO
Code:
Copy codeThe Code is as follows:
Person = {'name': 'lizhong ', 'age': '26', 'city': 'beijing', 'blog': 'www .jb51.net '}
For key, value in person. items ():
Print 'key = ', key,', value = ', value
Execution result:
It can be seen that the key receives the dictionary key, and the value receives the dictionary value.
But what if only one parameter is received?
Copy codeThe Code is as follows:
Person = {'name': 'lizhong ', 'age': '26', 'city': 'beijing', 'blog': 'www .jb51.net '}
For x in person. items ():
Print x
Execution result
If only one variable receives a value, each pair of key and value tuples are directly returned.
Using item () is similar to foreach in php. The key => value can be traversed. If you only use for... in, you can only obtain the key value of each pair of elements.
Such as code:
Copy codeThe Code is as follows:
Person = {'name': 'lizhong ', 'age': '26', 'city': 'beijing', 'blog': 'www .jb51.net '}
For x in person:
Print x
Execution result:
How can I traverse and set the value of a list in a dictionary in python?
For key in e:
For value in e [key]:
Print value
List traversal in python
There is no elegant solution, only do not use the print statement, as mentioned above, or use the print function in python3.X (through
From _ future _ import print_function enables the print function form)
In fact, the print statement is described in the python2.X Manual: (python2.7.2 official help Documentation)
A space is automatically printed before each object,
Unless: (1) no output is written to the standard output.
(2) When the last one is written to the standard output, it is a blank character except space ''.
(3) What is written to the standard output is not a print statement.
Therefore, there is a space before each character, such as apple and banana. (Apple's a is also free !)
A good solution is to use the print function in python3.X.
Add the following content before the file:
From _ future _ import print_function
You can use the print function format.
Syntax of the print function:
Print ([object,...] [, sep = ''] [, end = '\ n'] [, file = sys. stdout])
By default, if no sep is specified, spaces are used. If end is not specified, a line break is used. If no output file is specified, it is output to the standard output.
For example, print ('hello', 'World', sep = '-', end = '#') output:
Hello-world #
Therefore, your program can be changed:
From _ future _ import print_function
List = ["apple", "banana", "grape", "orange"]
For x in range (len (list )):
Print ('list [% d]: '% x, end = '')
For y in range (len (list [x]):
Print (list [x] [y], sep = '', end = '')
Print ('')
As for: the percent sign in 'list [% d]: '% x is a string operator. The percent sign makes the string before the percent sign
% D is replaced by the value of x after the percent sign.
Reference: python2.7.2 official help document.