比较列表项与python中另一个列表的索引

时间:2014-06-26 10:15:47

标签: python list

我想比较两个列表使用第一个列表的项目和第二个列表的索引,并且新列表将从第二个列表中追加每个匹配的列表。

a = [[1],[0],[0]]
b = [[1,2],[3,4],[5,6]]
c = []
for item in a:
     for i in range(len(b)):
          if item == b[i]:
              c.append(b[i])

答案应该是这样的:

c = [[3,4],[1,2],[1,2]]

5 个答案:

答案 0 :(得分:4)

最简单的:

c = [b[i[0]] for i in a]

我建议添加范围检查:

c = [b[i[0]] for i in a if (0 <= i[0] < len(b))]

编辑:根据您对a的说明,我建议您更改:

def findInstances(list1, list2):
    for i in list1:
        yield [pos for pos,j in enumerate(list2) if i==j] # This yields a list containing the value you want

为:

def findInstances(list1, list2):
    for i in list1:
        if (i in list2): 
            yield list2.index(i) # This yields only the value you want

这会使您的列表变得扁平化,并使问题更简单。然后,您可以使用以下内容:

c = [b[i] for i in a if (0 <= i < len(b))]

根据您获得a的方式,范围检查实际上是不必要的。我留下了他们,以防你以不同的方式得到a

答案 1 :(得分:2)

In [1]: a = [[1],[0],[0]]

In [2]: b = [[1,2],[3,4],[5,6]]

In [3]: [b[x[0]] for x in a] 
Out[3]: [[3, 4], [1, 2], [1, 2]]

答案 2 :(得分:2)

使用numpy索引:

>>a = np.asarray(a)
>>b = np.asarray(b)
>>b[a]
array([[[3, 4]],

       [[1, 2]],

       [[1, 2]]])

答案 3 :(得分:1)

您的算法几乎是正确的。问题在于if语句。如果您在测试相等性之前尝试打印itemb[i],则会看到问题。

>>> a = [[1],[0],[0]]
>>> b = [[1,2],[3,4],[5,6]]
>>> c = []
>>> for item in a:
>>>      for i in range(len(b)):
>>>           print("item == b[i] is {} == {} is {}".format(item, b[i], 
                      item == b[i]))
>>>           if item == b[i]:
>>>               c.append(b[i])
item == b[i] is [1] == [1, 2] is False
item == b[i] is [1] == [3, 4] is False
item == b[i] is [1] == [5, 6] is False
item == b[i] is [0] == [1, 2] is False
item == b[i] is [0] == [3, 4] is False
item == b[i] is [0] == [5, 6] is False
item == b[i] is [0] == [1, 2] is False
item == b[i] is [0] == [3, 4] is False
item == b[i] is [0] == [5, 6] is False

您基本上已经检查ab中的每个元素是否相等。而是希望检查a的每个项目中的元素是否与b的索引相等。

例如

for item_a in a:
    for index_b, item_b in enumerate(b):
        # only check index 0 of item_a as all lists are of length one.
        print("item_a[0] == index_b is {} == {} is {}".format(item_a[0], 
                  index_b, item_a[0] == index_b))
        if item_a[0] == index_b:
            c.append(item_b)

产生

item_a[0] == index_b is 1 == 0 is False
item_a[0] == index_b is 1 == 1 is True
item_a[0] == index_b is 1 == 2 is False
item_a[0] == index_b is 0 == 0 is True
item_a[0] == index_b is 0 == 1 is False
item_a[0] == index_b is 0 == 2 is False
item_a[0] == index_b is 0 == 0 is True
item_a[0] == index_b is 0 == 1 is False
item_a[0] == index_b is 0 == 2 is False

enumerate是一个内置辅助函数,它返回一个元组,其中包含列表中每个元素的索引和元素(或任何可迭代的元素)。

除非您需要,否则我还建议展平a,因为嵌套列表在这里是多余的,即。 a = [1, 0, 0]

说完这一切之后,如果你可以了解列表理解,那么编写解决方案就会简单得多 - 正如你问题的其他答案所证明的那样。

答案 4 :(得分:0)

这是我如何比较另外两个不同的列表。

def findInstances(list1, list2):
    for i in list1:
    yield [pos for pos,j in enumerate(list2) if i==j]


list1 = [0.1408, 0.1456, 0.2118, 0.2521, 0.1408, 0.2118]
list2 = [0.1408, 0.1456, 0.2118, 0.2521, 0.1254, 0.1243]
list3 = [[1,2],[3,4],[5,6],[7,8],[9,10],[11,12]]
res = list(findInstances(list1, list2))

并在第一个问题中将输出生成为'a'

谢谢