简体   繁体   English

如何打印字典中某个值的多个索引?

[英]How can I print multiple indexes of a certain value in a dictionary?

I'm just learning python and I have a problem.我刚刚学习 python,我遇到了问题。 how can I print multiple indexes of a certain value in a dictionary?如何打印字典中某个值的多个索引? In particular, I want to print the index of each element of the dictionary_title array which has gender_ids as the key.特别是,我想打印以gender_ids为键的 dictionary_title 数组的每个元素的索引。

dictionary_title={
{'label': 'Green', 'genre_ids': 878},
{'label': 'Pink', 'genre_ids': 16},
{'label': 'Orange', 'genre_ids': 28},
{'label': 'Yellow', 'genre_ids': 9648},
{'label': 'Red', 'genre_ids': 878},
{'label': 'Brown', 'genre_ids': 12},
{'label': 'Black', 'genre_ids': 28},
{'label': 'White', 'genre_ids': 14},
{'label': 'Blue', 'genre_ids': 28},
{'label': 'Light Blue', 'genre_ids': 10751},
{'label': 'Magenta', 'genre_ids': 28},
{'label': 'Gray', 'genre_ids': 28}}

This is my code:这是我的代码:

   for values in dictionary_title["genre_ids"]: 
   for item in values:       
       if item == 28:      
           print(values.index(item))  

For example, I want to print index:2,6,8,10,11 which are the indexes of the items with the key, genre_ids =28.例如,我想打印索引:2、6、8、10、11,它们是键为genre_ids =28 的项目的索引。 How can I do it?我该怎么做?

You can use a list comprehension with enumerate .您可以将列表理解与enumerate结合使用。

dictionary_title=[
{'label': 'Green', 'genre_ids': 878},
{'label': 'Pink', 'genre_ids': 16},
{'label': 'Orange', 'genre_ids': 28},
{'label': 'Yellow', 'genre_ids': 9648},
{'label': 'Red', 'genre_ids': 878},
{'label': 'Brown', 'genre_ids': 12},
{'label': 'Black', 'genre_ids': 28},
{'label': 'White', 'genre_ids': 14},
{'label': 'Blue', 'genre_ids': 28},
{'label': 'Light Blue', 'genre_ids': 10751},
{'label': 'Magenta', 'genre_ids': 28},
{'label': 'Gray', 'genre_ids': 28}]

res = [i for i, o in enumerate(dictionary_title) if o['genre_ids'] == 28]
print(res)

"dictionary title" must be a list:!! “字典标题”必须是一个列表:!! I leave you the code of how to do it :-)我给你留下了如何做的代码:-)

dictionary_title=[
{'label': 'Green', 'genre_ids': 878},
{'label': 'Pink', 'genre_ids': 16},
{'label': 'Orange', 'genre_ids': 28},
{'label': 'Yellow', 'genre_ids': 9648},
{'label': 'Red', 'genre_ids': 878},
{'label': 'Brown', 'genre_ids': 12},
{'label': 'Black', 'genre_ids': 28},
{'label': 'White', 'genre_ids': 14},
{'label': 'Blue', 'genre_ids': 28},
{'label': 'Light Blue', 'genre_ids': 10751},
{'label': 'Magenta', 'genre_ids': 28},
{'label': 'Gray', 'genre_ids': 28}]

for idx in range(0, len(dictionary_title)):
    if dictionary_title[idx]["genre_ids"] == 28:
        print(idx)

You can also do it using different loop styles.您也可以使用不同的循环 styles 来完成。

Style 2: Loop using enumerate()方式 2:使用 enumerate() 循环

for enumerate_item in enumerate(dictionary_title):
    if enumerate_item[1]["genre_ids"] == 28:
        print(enumerate_item[0])

Style 3: Loop using enumerate() just in other way...样式 3:以其他方式使用 enumerate() 循环...

for idx, dict in enumerate(dictionary_title):
    if dict['genre_ids'] == 28:
        print(idx)

Style 4: Loop using list comprehension样式 4:使用列表推导式循环

rest = [enumerate_item[0] for enumerate_item in enumerate(dictionary_title) if enumerate_item[1]['genre_ids'] == 28]
print(rest)

Style 5: Loop using list comprehension just in other way...样式 5:以其他方式使用列表推导式循环……

rest = [(idx) for idx, dict in enumerate(dictionary_title) if dict['genre_ids'] == 28]
print(rest)

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

相关问题 如何查找和打印列表中多个元素的索引? - How can I find and print the indexes of multiple elements in a list? 如何创建索引某些键位置的字典? - How do I create a dictionary that indexes the location of certain keys? 如何使打印词典的一部分打印出数学值? - How can I make printing a portion of a dictionary print the mathematical value? 如何在没有换行符 '\n' 的情况下打印字典值? - How can I print dictionary value without newline '\n' character? 如何在下面的代码中打印字典中的值? - How can I print a value from dictionary on the below code? 如果它们是基于字典的,如何在Python中打印变量的值? - How can I print the value of variables in Python if they are dictionary-based? 如何使用索引根据键和值将字典拆分为多个字典? - How to split dictionary according to key and value into multiple dictionaries using indexes? 如何使用索引列表索引多个列表的字典? - How do I index a dictionary of multiple lists using a list of indexes? 给定键适合特定间隔,如何在python中打印出字典的值? - How can I print out the values of a dictionary in python given the keys fit a certain interval? Python-如何从列表中的许多词典中打印某个词典字段? - Python - How can i print a certain dictionary field from many dictionaries within a list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM