Examples of the method of the dictionary traversal

There are two implementations of the method of the dictionary traversal, exemplified below

method 1.

dic1 = {'Europe':{'man':'Guido','woman':'girl'},
       'Asia':{'man':'Gongzhen','woman':'Xudan'} }
# for i in dic1:
#     print(i,dic1[i])
for i in dic1.items():
    print(i)

Method 2.

dic1 = {'Europe':{'man':'Guido','woman':'girl'},
       'Asia':{'man':'Gongzhen','woman':'Xudan'} }
for i in dic1:
    print(i,dic1[i])
# for i in dic1.items():
#     print(i)

The results obtained by the same two methods as follows:

 

3. The keys and values ​​may be stored respectively with two variables

dic1 = {'Europe':{'man':'Guido','woman':'girl'},
       'Asia':{'man':'Gongzhen','woman':'Xudan'} }
# for i in dic1:
#     print(i,dic1[i])
# for i in dic1.items():
#     print(i)
for i,v in dic1.items():
    print(i,v)

The results are as follows:

 

Guess you like

Origin www.cnblogs.com/iceberg710815/p/11862786.html