[英]How to get the index of a dictionary inside list if a specific key item is in the dictionary
I have a list intents_list
this list contains 3 dictionaries, each dictionary has a tag (a key), for example intents_list[0]
has the tag hello
.我有一个列表
intents_list
这个列表包含 3 个字典,每个字典都有一个标签(一个键),例如intents_list[0]
有标签hello
。 I want to check what index has a specific tag.我想检查哪个索引具有特定标签。
intents_list = [{'tag': 'hello', 'patterns': ['Hello', 'Hello there'], 'responses': ['hi!', 'wassup!']}, {'tag': 'goodnight', 'patterns': ['goodnight my friend', 'wish you a good night'], 'responses': ['you two!', 'goodnight!']}]
tagstring = 'goodnight'
What I tried to do was to join the list arguments and check if they contain the tag, it didn't work and it's not exactly what I want to do.我试图做的是加入列表 arguments 并检查它们是否包含标签,它不起作用,这不是我想要做的。 I want to only check if tag contains
tagstring
我只想检查标签是否包含标签
tagstring
index = [intents_list.index(index) for index in intents_list if tagstring in ''.join(intents_list[intents_list.index(index)])]
Use a list comprehension to get the indices:使用列表推导获取索引:
indices = [i for i, d in enumerate(intents_list) if d['tag'] == tagstring]
Something like this will work:像这样的东西会起作用:
index = next(iter([i for i, x in enumerate(intents_list) if x[‘tag’] ==
tagString]), None)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.