10

我有两个 python 列表:

a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]

b = ['the', 'when', 'send', 'we', 'us']

我需要从a中过滤掉所有与b中相似的元素。就像在这种情况下,我应该得到:

c = [('why', 4), ('throw', 9), ('you', 1)]

最有效的方法应该是什么?

4

7 回答 7

11

列表理解将起作用。

a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
b = ['the', 'when', 'send', 'we', 'us']
filtered = [i for i in a if not i[0] in b]

>>>print(filtered)
[('why', 4), ('throw', 9), ('you', 1)]
于 2013-02-23T09:30:49.137 回答
5

列表理解应该起作用:

c = [item for item in a if item[0] not in b]

或者使用字典理解:

d = dict(a)
c = {key: value for key in d.iteritems() if key not in b}
于 2013-02-23T09:28:40.310 回答
2

in很好,但您至少应该使用集合b。如果您有 numpy,np.in1d当然也可以尝试,但如果它更快与否,您可能应该尝试。

# ruthless copy, but use the set...
b = set(b)
filtered = [i for i in a if not i[0] in b]

# with numpy (note if you create the array like this, you must already put
# the maximum string length, here 10), otherwise, just use an object array.
# its slower (likely not worth it), but safe.
a = np.array(a, dtype=[('key', 's10'), ('val', int)])
b = np.asarray(b)

mask = ~np.in1d(a['key'], b)
filtered = a[mask]

集合也有方法difference等,这些在这里可能没用,但总的来说可能有用。

于 2013-02-23T11:11:48.747 回答
2

由于这是用 标记的,因此这是一个使用列表理解基准测试numpy的 numpy 解决方案:numpy.in1d

In [1]: a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]

In [2]: b = ['the', 'when', 'send', 'we', 'us']

In [3]: a_ar = np.array(a, dtype=[('string','|S5'), ('number',float)])

In [4]: b_ar = np.array(b)

In [5]: %timeit filtered = [i for i in a if not i[0] in b]
1000000 loops, best of 3: 778 ns per loop

In [6]: %timeit filtered = a_ar[-np.in1d(a_ar['string'], b_ar)]
10000 loops, best of 3: 31.4 us per loop

因此,对于 5 条记录,列表理解更快。

然而,对于大型数据集,numpy 解决方案的速度是列表理解的两倍:

In [7]: a = a * 1000

In [8]: a_ar = np.array(a, dtype=[('string','|S5'), ('number',float)])

In [9]: %timeit filtered = [i for i in a if not i[0] in b]
1000 loops, best of 3: 647 us per loop

In [10]: %timeit filtered = a_ar[-np.in1d(a_ar['string'], b_ar)]
1000 loops, best of 3: 302 us per loop
于 2013-02-24T10:37:49.277 回答
0

试试这个 :

a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]

b = ['the', 'when', 'send', 'we', 'us']

c=[]

for x in a:
    if x[0] not in b:
        c.append(x)
print c

演示:http: //ideone.com/zW7mzY

于 2013-02-23T09:30:42.093 回答
0

简单的方法

a = [('when', 3), ('why', 4), ('throw', 9), ('send', 15), ('you', 1)]
b = ['the', 'when', 'send', 'we', 'us']
c=[] # a list to store the required tuples 
#compare the first element of each tuple in with an element in b
for i in a:
    if i[0] not in b:
        c.append(i)
print(c)
于 2020-04-17T11:11:09.157 回答
-1

使用过滤器:

c = filter(lambda (x, y): False if x in b else True, a)
于 2013-02-23T09:33:23.707 回答