# for user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi' } for key, value in user_0.items(): print("key: " + key + " | value: " + value) #output: key: username | value: efermi key: first | value: enrico key: last | value: fermi
3.2遍历所有键
1 2 3 4 5 6 7 8 9 10 11
user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi' } for key in user_0.keys(): print(key.title()) #output: Username First Last
3.3按顺序遍历所有键
1 2 3 4 5 6 7 8 9 10 11
user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi' } for key insorted(user_0.keys()): print(key.title()) #output: First Last Username
3.4遍历所有值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
user_0 = { 'username': 'efermi', 'first': 'enrico', 'last': 'fermi' } for value in user_0.values(): print(value.title()) #output: Efermi Enrico Fermi #use set drop same value 删除重复元素 user_0['middle'] = 'fermi' for value inset(user_0.values()): print(value.title()) #output: Efermi Enrico Fermi