程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Dictionary (2) - nested

編輯:Python

1 nesting

There will be many Dictionaries are stored in lists in , Or will List as value Store in a dictionary .

2 Save dictionary in list

establish 3 A dictionary alien_0,alien_1,alien_2; Store the dictionary in a list aliens in :

# Create a dictionary
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':10}
alien_2={'color':'red','points':15}
# Nested dictionaries in the list
aliens=[alien_0,alien_1,alien_2]
# Print
for alien in aliens:
print(alien)

Running results :
{'color': 'green', 'points': 5}
{'color': 'yellow', 'points': 10}
{'color': 'red', 'points': 15}

Create more dictionaries , Save list aliens in :

aliens=[];
for aliens_num in range(0,10):
new_aliens={'color':'green','point':5,'speed':'slow'}
aliens.append(new_aliens)
for alien in aliens[:4]:# Before printing 4 individual aliens
print(alien)
print('.....')
print(f" Altogether {len(aliens)} individual alien")

Running results :

{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
.....
Altogether 10 individual alien

3 Keep a list in the dictionary

establish , Nested list in dictionary :

vehicle_info={'Number':1001,
'variable':['nmot_w','rl_w','miist_w']}
print(f"Vehicle {vehicle_info['Number']} measured variable: {vehicle_info['variable']}")

Running results :

Vehicle 1001 measured variable: ['nmot_w', 'rl_w', 'miist_w']

4 Nesting with each other

Nested dictionaries in the list , Nested list in dictionary :

# Nested list in dictionary
vehicle_info_0={'Number':1001,
'variable':['nmot_w','rlsol_w','miist_w']}
vehicle_info_1={'Number':1002,
'variable':['nmot_w','rl_w','miist_w']}
vehicle_info_2={'Number':1003,
'variable':['nmot_w','rlp_w','miist_w']}
vehicle_info_3={'Number':1004,
'variable':['nmot_w','rl_w','miist_w']}
# Nested dictionaries in the list
vehicle_infos=[vehicle_info_0,vehicle_info_1,vehicle_info_2,vehicle_info_3]
for vehicle_info in vehicle_infos:# Traverse the list of vehicles
for value in vehicle_info['variable']:# Traverse the measured variables in the vehicle list
if value == 'rl_w':# Look for something that contains rl_w Variable vehicle , And print it out
print(f"rl_w is within the {vehicle_info['Number']} measurement variable.")

Running results :

rl_w is within the 1002 measurement variable.
rl_w is within the 1004 measurement variable.

Dictionary nested dictionary :

# Nested list in dictionary
vehicle_info_0={'Number':1001,
'variable':['nmot_w','rlsol_w','miist_w']}
vehicle_info_1={'Number':1002,
'variable':['nmot_w','rl_w','miist_w']}
vehicle_info_2={'Number':1003,
'variable':['nmot_w','rlp_w','miist_w']}
vehicle_info_3={'Number':1004,
'variable':['nmot_w','rl_w','miist_w']}
# Nested dictionaries in dictionaries
vehicle_infos= {
'jack':vehicle_info_0,
'allen':vehicle_info_1,
'lucy':vehicle_info_2,
'jhon':vehicle_info_3
}
for vehicle_info in vehicle_infos.keys():# Traverse the vehicle owner key
if vehicle_infos[vehicle_info]['Number'] == 1003:# seek 1003 Number of the vehicle owner , And print it out
print(f"Vehicle 1003 is belone to {vehicle_info} .")

Running results :

Vehicle 1003 is belone to  lucy .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved