python - 遍历字典

标签 python iteration python-2.x

使用 print(i, j)print(i) 的两个设置返回相同的结果。有没有一个案例 应该在另一个之上使用还是可以互换使用它们是否正确?

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 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)

关于python - 遍历字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48585577/

相关文章:

python - 如何在python中打印百分比值?

Python:如何使用变量传递参数名称

python - 如何将大型 python 模型应用于 pyspark-dataframe?

java - 迭代方法: Delete Linked List node using only one reference variable

python - 是否有任何 Python2 的实现可以传递排序?

string - 用C API提取python str,兼容python 2&3

python / Pandas : Counting the number of times a value less than x appears in a column

python - 将数组形状转换为字符串

信号的 Numpy 均方根 (RMS) 平滑

r - 对数据应用函数 n 次