如何在数据集a中找到'node4'的索引?我希望node4成为这个特定函数的输入。
前任:

a = [
   {'node1':['node2','node3','node5']},
   {'node2':['node1','node8','node10']},
   {'node3':['node4','node2']},
   {'node4':['node2','node1','node3']},
   {'node5':['DEADEND']},
   {'node6':['GOAL']}
    ....
    ]

最佳答案

Use nextand enumerate

>>> next((i for i,x in enumerate(a) if 'node4' in x), None)
3

关于python - 通过键值在列表中查找字典的索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20345693/

10-14 01:12