Python:从字典列表中查找字典索引

Python:从字典列表中查找字典索引,python,list,dictionary,indexing,Python,List,Dictionary,Indexing,我有一份字典清单。如何找到特定词典的索引?例如: [{'pop': array([1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0,

我有一份字典清单。如何找到特定词典的索引?例如:

[{'pop': array([1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0,
         1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1,
         1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
         1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
         0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1]),
  'weight': 260.1,
  'value': 2313},
 {'pop': array([1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
         0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
         0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,
         0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0,
         0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1]),
  'weight': 235.60000000000002,
  'value': 2774},
 {'pop': array([0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0,
         0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
         1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
         0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
         1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1]),
  'weight': 192.0,
  'value': 2254},
 {'pop': array([0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
         1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1,
         0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0,
         1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1,
         0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1]),
  'weight': 264.29999999999995,
  'value': 2813}]
这是字典目录。我想从列表中查找此词典的索引:

{'pop': array([0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0,
             0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
             1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
             0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
             1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1]),
      'weight': 192.0,
      'value': 2254}

python中的
列表
类型是可变的,这意味着对象本身不能保证在转换过程中保持顺序。因此,应谨慎使用这种方法

有了这些,你就可以做类似的事情了

input_list=[{'pop':[1,0,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,0,0,1,0,
1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1,
1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1],
“重量”:260.1,
“值”:2313},
{'pop':[1,1,0,1,1,1,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,1,0,
0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,
0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0,
0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1],
“重量”:235.6000000000002,
“值”:2774},
{'pop':[0,1,1,0,1,1,0,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,
0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
“权重”:192.0,
“值”:2254},
{'pop':[0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,0,0,1,1,1,1,0,1,
1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1,
0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0,
1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1,
0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
“重量”:264.29999995,
“值”:2813}]
elem={'pop':[0,1,1,1,0,1,1,0,0,0,0,0,1,1,1,0,0,0,1,1,1,0,0,0,
0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
“权重”:192.0,
“值”:2254}
out=next((i代表i,x在枚举(输入列表)中,如果x==elem),“未找到元素”)
打印(输出)

python3中的列表有一个
索引
函数,您可以使用该函数查找特定元素的索引-

my_list = [{'pop': [1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 0,
         1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1,
         1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
         1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 1,
         0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1],
  'weight': 260.1,
  'value': 2313},
 {'pop': [1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0,
         0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 1,
         0, 0, 1, 0, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 1,
         0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0,
         0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 1],
  'weight': 235.60000000000002,
  'value': 2774},
 {'pop': [0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0,
         0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
         1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
         0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
         1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
  'weight': 192.0,
  'value': 2254},
 {'pop':[0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 1, 0, 1,
         1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1,
         0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 0, 1, 0,
         1, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 1,
         0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1],
  'weight': 264.29999999999995,
  'value': 2813}]

my_dict = {'pop': [0, 1, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 0, 0,
             0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1,
             1, 0, 0, 1, 0, 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0,
             0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1,
             1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1],
      'weight': 192.0,
      'value': 2254}

print(my_list.index(my_dict))

请记住,它只会在存在多个匹配项的情况下返回第一个匹配项(因为列表可以有重复的元素)

如果您确实需要字典中的数组而不是列表,则需要编写自定义比较器,因为该方法(由==运算符调用)numpy.array的行为不同

您将收到以下错误:

ValueError: The truth value of an array with more than one element is ambiguous. 

Use a.any() or a.all()
它不会比较整个数组并返回布尔答案,而是返回元素级比较(数组):

因此,对于数组中的单个布尔答案,我们需要显式调用该方法:

import numpy as np

def elements_equal(x, y):
    if x is None and y is None: return True
    
    if x is not None and y is not None:
        if x.keys() != y.keys(): return False
        for key in x.keys():
            if type(x[key]) is np.ndarray:
                if not np.array_equal(x[key], y[key]): return False
            elif x[key] != y[key]: return False
        return True

    return False
然后扩展到oskros:


我想这也许可以回答你的问题:你有没有尝试过你的list.index(IteemYouSearch)?
import numpy as np

def elements_equal(x, y):
    if x is None and y is None: return True
    
    if x is not None and y is not None:
        if x.keys() != y.keys(): return False
        for key in x.keys():
            if type(x[key]) is np.ndarray:
                if not np.array_equal(x[key], y[key]): return False
            elif x[key] != y[key]: return False
        return True

    return False
out = next((i for i, x in enumerate(dict_list) if elements_equal(x, e)), "Element not found")
print(out)