简体   繁体   English

如何使用for循环打印字典中包含的列表的元素

[英]How do I print elements of a list contained in a dictionary using a for loop

I have a dictionary: 我有一本字典:

FFA = {'House': ['0.511', '0.374', 10], 'Chair': ['0.704', '0.381', 10], 'Shoe': ['0.922', '0.465', 10], 'Bottle': ['0.764', '0.348', 10], 'Face': ['1.084', '0.373', 10]}

I want to print certain elements in a for loop: 我想在for循环中打印某些元素:

print "ROI", "Cope", "Mean", "Stddev", "Nsamples"
for k in FFA:
    print "FFA", k, elem[0], elem[1], elem[2]

That's my output: 那是我的输出:

 ROI Cope Mean Stddev Nsamples
FFA House 1.084 0.373 10
FFA Chair 1.084 0.373 10
FFA Shoe 1.084 0.373 10
FFA Bottle 1.084 0.373 10
FFA Face 1.084 0.373 10

However, my code is so wrong I can't find a way of iterating over all the keys in the dic so the right values are printed out. 但是,我的代码太错了,我找不到在dic中所有键上进行迭代的方法,因此无法正确打印出值。

I want my output to be: 我希望我的输出是:

ROI Cope Mean Stddev Nsamples
FFA House 0.511 0.374 10
FFA Chair 0.704 0.381 10
FFA Shoe  0.922 0.465 10
FFA Bottle 0.764 0.348 10
FFA Face 1.084 0.373 10

You were close: 您接近:

print "ROI", "Cope", "Mean", "Stddev", "Nsamples"
for k in FFA:
    elem = FFA[k]
    print "FFA", k, elem[0], elem[1], elem[2]

or directly: 或直接:

print "ROI", "Cope", "Mean", "Stddev", "Nsamples"
for k in FFA:
    print "FFA", k, FFA[k][0], FFA[k][1], FFA[k][2]

Use items() to iterate over the keys and the values. 使用items()遍历键和值。 Since you have multiple values, iterate over the values using the nested for loop over multiple values. 由于您具有多个值,因此可以使用嵌套的for循环遍历多个值来遍历这些值。 For python 2.x it will be print "FFA", k, values, 对于python 2.x,它将print "FFA", k, values,

print "ROI", "Cope", "Mean", "Stddev", "Nsamples" # Header

for k, v in FFA.items():
    # print ("FFA", k,  end=" ") # For python 3.x
    print "FFA", k,
    for values in v:
        print values,
        # print (values, end=" ") # For python 3.x
    print # To get to print to the new line
    # print ()  # For python 3.x

ROI Cope Mean Stddev Nsamples
FFA House 0.511 0.374 10 
FFA Chair 0.704 0.381 10 
FFA Shoe 0.922 0.465 10 
FFA Bottle 0.764 0.348 10 
FFA Face 1.084 0.373 10 

You already loop over the keys, now you simply need to loop over the elements of the values: 您已经遍历了键,现在只需要遍历值的元素:

FFA = {'House': ['0.511', '0.374', 10], 
       'Chair': ['0.704', '0.381', 10], 
       'Shoe': ['0.922', '0.465', 10], 
       'Bottle': ['0.764', '0.348', 10], 
      'Face': ['1.084', '0.373', 10]} 

print "ROI", "Cope", "Mean", "Stddev", "Nsamples"


for k in FFA:      # loop over keys in dict 
    print "FFA", k,     # , at end == no newline
    for elem in FFA[k]: # loop over values in list of key
        print elem,         # , at end == no newline
    print               # now a newline

Output: 输出:

ROI Cope Mean Stddev Nsamples
FFA House 0.511 0.374 10
FFA Chair 0.704 0.381 10
FFA Shoe 0.922 0.465 10
FFA Bottle 0.764 0.348 10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查列表中的元素是否包含在其他列表中? - How do i check if the elements in a list are contained in other list? 如何在for循环中删除字典中的元素 - How do I delete elements in dictionary in a for loop 如何在Python的列表中打印字典的键和值 - How to print keys and values of a dictionary which is contained in a list in Python 如何在 for 循环中将元素添加到列表并打印单个列表? - How can I add elements to a list in a for loop and print a single list? 我有嵌套列表作为Python字典的条目。 如何打印第一个元素为“S”的嵌套列表? - I have nested lists as entries to a Python dictionary. How do I print the nested list whose first elements is “S”? 如何确定一个列表是否包含另一个列表中的项目,然后在 Python 中打印它们的索引 - how do I determine if one list has items contained in another list, then print their index in Python 如何使用列表中的元素遍历字典中的值 - How to loop over a value in a dictionary using elements in a list 如何使用for循环将存储在字典中的变量打印到屏幕上? - How can I print a variables stored in a dictionary to screen using a for loop? 如何从文本文件中打印字典的排序列表? - How do I print a sorted list of a dictionary from a text file? 如何在列表和字典中打印带有“for loops”的项目数? - How do I print the number of items with 'for loops' in a list and dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM