for k, v in user.items()
user = {
'username': 'efermi',
'first': 'enrico',
'last': 'fermi',
}
for key, value in user.items():
print("\nKey :" + key)
print("Value: " + value)
out:
Key :last
Value: fermi
Key :first
Value: enrico
Key :username
Value: efermi
person_phone = {
'A': 'huawei',
'B': 'apple',
'C': 'meizu',
'D': 'xiaomi',
}
friends = ['A', 'D']
###对返回的键进行排序
for name in sorted(person_phone.keys()):
print(name.title())
if name in friends:
print(name.title() + "手机是 " +
person_phone[name].title() + "!")
out:
A
A手机是 Huawei!
B
C
D
D手机是 Xiaomi!
###收集顺序输出
from collections import OrderedDict
d = OrderedDict([('a', 1), ('b', 2), ('d', 4), ('c', 3)])
for k,v in d.items():
print(k,v)
out:
a 1
b 2
d 4
c 3