根据元素值从嵌套列表中删除子列表

时间:2016-01-12 02:47:43

标签: python string list

我正在尝试根据元素值从嵌套列表中删除子列表。

data = [['1', 'i like you'],
['2', 'you are bad'],
['5', 'she is good'],
['7', 'he is poor']]

negative_words = set(['poor', 'bad'])

如果第二列包含否定词,我想删除子列表。因此,期望的结果如下。有什么建议吗?

data = [['1', 'i like you'],
['5', 'she is good']]

1 个答案:

答案 0 :(得分:1)

我很容易想到两种方式。

>>> data = [['1', 'i like you'], ['2', 'you are bad'],
            ['5', 'she is good'], ['7', 'he is poor']]
>>> neg_words = {'poor', 'bad'}

将子列表中的转换字符串用于集合,然后使用neg_words检查它是disjoint,如下所示

>>> [[n, ws] for n, ws in data if set(ws.split()).isdisjoint(neg_words)]
[['1', 'i like you'], ['5', 'she is good']]

或者只是检查列表中字符串中的单词是否都在neg_words中,就像这样

>>> [[n, ws] for n, ws in data if all(w not in neg_words for w in ws.split())]
[['1', 'i like you'], ['5', 'she is good']]