使用列表推导来搜索列表

时间:2016-01-04 07:03:20

标签: python list-comprehension

我正在比较两个列表,以查找是否已将新数据添加到数据库' polo'。

最初这些名单采用相同的形式,但现在' excel'需要一个伴随的坐标来衡量它的价值。这是这个原始的LC:

[x for x in polo_list if x not in excel]

我很高兴听到我们能够解决这个问题的不同方法(也许我采取了错误的方法),现在这里是代码示例:

excel = [ ['a','a4'],['b','z4']]
polo = ['a','b','d']

a = [x for x in polo if x not in excel]

print 'new data! ', a

#should print,'new data!' ['d']

感谢您的时间

编辑:啊,幻想!它现在看起来如此简单!谢天谢地stackoverflow社区,我freakin'喜欢这个网站

4 个答案:

答案 0 :(得分:4)

只需搜索另一种理解:

a = [x for x in polo if x not in [item[0] for item in excel]]

最好事先保存这些值:

excel_first = [item[0] for item in excel]
a = [x for x in polo if x not in excel_first]

或使用set

excel_first = {item[0] for item in excel}
a = [x for x in polo if x not in excel_first]

或者,更好的是,使用字典:

excel = dict(excel)
a = [x for x in polo if x not in excel]

excel将成为一个字典,您可以使用它来快速查找坐标。

答案 1 :(得分:1)

我建议在这里使用两个列表理解:

>>> excel = [['a', 'a4'], ['b', 'z4']]
>>> polo = ['a','b','d']
>>> [x for x in polo if x not in [i[0] for i in excel]]
['d']

[i[0] for i in excel]生成一个新的['a', 'b']列表,它循环遍历[['a', 'a4'], ['b', 'z4']]并从中获取索引为0的元素。

无论如何,如果您在excel中有重复的,例如它是[['a', 'a4'], ['b', 'z4'], ['b', 'z5'], ['b', 'z7']],则[i[0] for i in excel]的输出将为['a', 'b', 'b', 'b']

由于这些'b'没用,我们可以使用集合理解:

>>> excel = [['a', 'a4'], ['b', 'z4']]
>>> polo = ['a','b','d']
>>> [x for x in polo if x not in {i[0] for i in excel}]
['d']

你也可以忘记列表理解和比较集,如下所示:

>>> excel = [['a', 'a4'], ['b', 'z4']]
>>> polo = ['a','b','d']
>>> set(polo) - {i[0] for i in excel}
{'d'}

请记住,这种方式不会保留顺序和重复键。

答案 2 :(得分:1)

itertools.chain可以提供帮助:

>>> from itertools import chain
>>> [x for x in polo if x not in chain(*excel)]
['d']
chain(*iterables) --> chain object
     

返回一个链式对象,其.__next__()方法返回来自的元素   第一次迭代,直到它耗尽,然后从下一个元素   迭代,直到所有的迭代都用完为止。

答案 3 :(得分:0)

您甚至可以使用filterchain

执行此操作
>>> from itertools import chain
>>> filter(lambda s:s not in chain(*excel), polo)
['d']

itemgettermap的另一种方式:

>>> from operator import itemgetter
>>> [x for x in polo if x not in map(itemgetter(0),excel)]
['d']

或者:

>>> [x for x in polo if x not in (itemgetter(0)(x) for x in excel)]
['d']
相关问题