[英]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.