遍历字典
Iterate Over Dictionary
使用print(i, j)
和print(i)
return的两种设置结果相同。有没有一个案例
应该在另一个之上使用还是可以互换使用它们是否正确?
desc = {'city': 'Monowi', 'state': 'Nebraska', 'county':'Boyd', 'pop': 1}
for i, j in desc.items():
print(i, j)
for i in desc.items():
print(i)
for i, j in desc.items():
print(i, j)[1]
for i in desc.items():
print(i)[1]
在 Python 3 中,print(i, j)
和 print(i)
与 return 的结果相同。
print(i, j)
打印键 i
后跟它的值 j
.
print(i)
打印包含字典键及其值的元组。
items()
returns 允许您迭代 (key, value)
元组的视图对象。所以基本上你可以像处理元组一样操纵它们。 document 可能有帮助:
iter(dictview)
Return an iterator over the keys, values or items (represented as tuples > of (key, value)) in the dictionary.
我还认为 print(i, j)[1]
会导致 Python 3 中出现错误,因为 print(i, j)
returns None
.
如果删除打印中的括号,两者是不同的,因为您使用的是 python 2X
desc = {'city': 'Monowi', 'state': 'Nebraska', 'county':'Boyd', 'pop': 1}
for i, j in desc.items():
print i, j
for i in desc.items():
print i
输出
county Boyd
city Monowi
state Nebraska
pop 1
('county', 'Boyd')
('city', 'Monowi')
('state', 'Nebraska')
('pop', 1)
使用print(i, j)
和print(i)
return的两种设置结果相同。有没有一个案例
应该在另一个之上使用还是可以互换使用它们是否正确?
desc = {'city': 'Monowi', 'state': 'Nebraska', 'county':'Boyd', 'pop': 1}
for i, j in desc.items():
print(i, j)
for i in desc.items():
print(i)
for i, j in desc.items():
print(i, j)[1]
for i in desc.items():
print(i)[1]
在 Python 3 中,print(i, j)
和 print(i)
与 return 的结果相同。
print(i, j)
打印键 i
后跟它的值 j
.
print(i)
打印包含字典键及其值的元组。
items()
returns 允许您迭代 (key, value)
元组的视图对象。所以基本上你可以像处理元组一样操纵它们。 document 可能有帮助:
iter(dictview) Return an iterator over the keys, values or items (represented as tuples > of (key, value)) in the dictionary.
我还认为 print(i, j)[1]
会导致 Python 3 中出现错误,因为 print(i, j)
returns None
.
如果删除打印中的括号,两者是不同的,因为您使用的是 python 2X
desc = {'city': 'Monowi', 'state': 'Nebraska', 'county':'Boyd', 'pop': 1}
for i, j in desc.items():
print i, j
for i in desc.items():
print i
输出
county Boyd
city Monowi
state Nebraska
pop 1
('county', 'Boyd')
('city', 'Monowi')
('state', 'Nebraska')
('pop', 1)