Using Python for loop in dictionaries and dictionary together

1. First define the contents of a dictionary

 1 i=
 2 {
 3         'status': 'success',
 4          'country': '中国',
 5          'countryCode': 'CN',
 6          'region': 'BJ'
 7 }

2. Print the dictionary to see

1 i=
2 {
3        'status': 'success',
4         'country': '中国',
5        'countryCode': 'CN',
6         'region': 'BJ'
7 }
8 print(i)

 

 

3. If the direct use of the FOR loop, then only the key words are displayed, the value of which will not be displayed

1 i={
2        'status': 'success',
3         'country': '中国',
4        'countryCode': 'CN',
5         'region': 'BJ'
6 }
7 for a in i:
8     print(a)

 

4. In the display value can be added .values ​​() after the dictionary, but not to display keyword

1 i={
2        'status': 'success',
3         'country': '中国',
4        'countryCode': 'CN',
5         'region': 'BJ'
6 }
7 for a in i.values():
8     print(a)

 

The keywords and values ​​can be displayed simultaneously in the dictionary after adding .items ()

1 i={
2        'status': 'success',
3         'country': '中国',
4        'countryCode': 'CN',
5         'region': 'BJ'
6 }
7 for a in i.items():
8     print(a)

 

6 .最好的方法还是加上key,value这样可以显示更多东西

1 i={
2        'status': 'success',
3         'country': '中国',
4        'countryCode': 'CN',
5         'region': 'BJ'
6 }
7 for key,value in i.items():
8     print("IP信息:"+str(key)+" is "+str(value))

 

7.如果只是字符串就可以单单这样显示即可

1 i={
2        'status': 'success',
3         'country': '中国',
4        'countryCode': 'CN',
5         'region': 'BJ'
6 }
7 for key,value in i.items():
8     print("IP信息:"+key+" is "+value)

 

 

参加文档:https://jingyan.baidu.com/article/636f38bb9b152dd6b8461025.html

 

Guess you like

Origin www.cnblogs.com/lanyincao/p/11857878.html