Python 从给定集合中出现的列表列表中删除所有单词

Python 从给定集合中出现的列表列表中删除所有单词,python,list,set,Python,List,Set,我试图用Python高效地完成三个简单步骤 我有一个列表(字符串)。让我们称之为L 我想将列表列表展平为一个列表LL。(我知道如何有效地做到这一点) 从步骤1的列表LL中构造频率为1的单词集。让我们把这个集合称为S。(我也知道怎么做。) (有效地) 删除列表L中出现在S中的所有单词 如果你能建议一种有效的方法来完成第3步,那将是一个很大的帮助。使用简单的列表理解来完成第3步: import collections import operator LL = reduce(operator.add

我试图用Python高效地完成三个简单步骤

我有一个列表(字符串)。让我们称之为
L

  • 我想将列表列表展平为一个列表LL。(我知道如何有效地做到这一点)

  • 从步骤1的列表LL中构造频率为1的单词集。让我们把这个集合称为S。(我也知道怎么做。) (有效地)

  • 删除列表L中出现在S中的所有单词


  • 如果你能建议一种有效的方法来完成第3步,那将是一个很大的帮助。

    使用简单的
    列表理解来完成第3步:

    import collections
    import operator
    
    LL = reduce(operator.add, L)
    counted_L = collections.Counter(LL)
    def filter_singles(sublist):
      return [value for value in sublist if counted_L[value] != 1]
    no_single_freq_L = [filter_singles(sublist) for sublist in L]
    
    >>> from collections import Counter
    >>> from itertools import chain
    >>> L=[['a','b'],['foo','bar'],['spam','eggs'],['b','c'],['spam','bar']]
    >>> S=Counter(chain(*L))
    >>> S
    Counter({'b': 2, 'bar': 2, 'spam': 2, 'a': 1, 'c': 1, 'eggs': 1, 'foo': 1})
    
    >>> [[y for y in x if S[y]!=1] for x in L]
    [['b'], ['bar'], ['spam'], ['b'], ['spam', 'bar']]
    
    如果您有一套
    R

    >>> L=[['a','b'],['foo','bar'],['spam','eggs'],['b','c'],['spam','bar']]
    >>> R={'a','foo'}
    >>> [[y for y in x if y not in R] for x in L]
    [['b'], ['bar'], ['spam', 'eggs'], ['b', 'c'], ['spam', 'bar']]
    

    您已经在步骤2中提到创建集合。内置类型可以使步骤3非常容易阅读和理解

    # if you are already working with sets:
    LL - S
    
    # or convert to sets
    set(LL) - set(S)
    
    快速示例

    >>> all_ten = set(range(0,10))
    >>> evens = set(range(0,10,2))
    >>> odds = all_ten - evens
    >>> odds
    set([0, 8, 2, 4, 6,])
    

    仔细检查邮件
    LL
    是扁平版本。通过调用
    Counter(L)
    我们实际上得到了某种与
    LL
    等价的值,每个值都有计数。不,你不需要,你需要先将列表展平。如果
    L
    是一个列表列表,那么
    Counter(L)
    无论如何都不会工作;列表不可散列。非常感谢,Bossylobster。另一个相关的问题是,如果已经给出了集合S,如何执行步骤3。请让我知道。致以最诚挚的问候,只需更换
    counted_L[value]!=1
    by
    值不在S
    中,假设
    S
    是Python中
    set
    的一个实例。顺便说一句,如果你喜欢我的回答,你应该接受它。非常感谢,阿什维尼。另一个相关的问题是,如果已经给出了集合S,如何执行步骤3。请让我知道。向你问好,我一定会接受你的回答。请告诉我怎么做。也请回答“相关问题”。这将是一个很大的帮助。致以最良好的祝愿,@Dibyendu相关问题在哪里?谢谢,Ashwini。假设我有一个给定的集合R(R已经被构造),我想从列表L中删除所有出现在R中的单词。我如何有效地做到这一点?致以最诚挚的问候,我认为列表理解既简单又有效:
    [[y代表y,如果y不在R]代表x,如果y不在L]
    非常感谢,Istruble。另一个相关的问题是,如果已经给出了集合S,如何执行步骤3。请让我知道。顺致敬意,
    >>> #Tools Needed
    >>> import collections
    >>> import itertools
    >>> #Just for this example
    >>> import keyword
    >>> import random
    >>> #Now create your example data
    >>> L = [random.sample(keyword.kwlist,5) for _ in xrange(5)]
    >>> #Flatten the List (Step 1)
    >>> LL = itertools.chain(*L)
    >>> #Create Word Freq (Step 2)
    >>> freq = collections.Counter(LL)
    >>> #Remove all words with unit frequency (Step 3)
    >>> LL = itertools.takewhile(lambda e:freq[e] > 1,freq)
    >>> #Display Your result
    >>> list(LL)
    ['and']
    >>> L
    [['in', 'del', 'if', 'while', 'print'], ['exec', 'try', 'for', 'if', 'finally'], ['and', 'for', 'if', 'print', 'lambda'], ['as', 'for', 'or', 'return', 'else'], ['and', 'global', 'or', 'while', 'lambda']]
    >>>