我有一个 list intents_list
这个列表包含3个字典,每个字典都有一个标签(一个键),例如intents_list[0]
具有标签hello
。我想检查哪个索引具有特定标签。
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'
我试图做的是加入列表参数并检查它们是否包含标签,但它不起作用,而且也不完全是我想要做的。我只想检查标签是否包含 tagstring
index = [intents_list.index(index) for index in intents_list if tagstring in ''.join(intents_list[intents_list.index(index)])]
最佳答案
使用列表理解来获取索引:
indices = [i for i, d in enumerate(intents_list) if d['tag'] == tagstring]
关于python - 如果字典中存在特定键项,如何获取列表中字典的索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66598176/