【问题标题】:Find unique words in a list of lists in python在python中的列表列表中查找唯一单词
【发布时间】:2022-01-13 02:57:22
【问题描述】:

我有一个列表列表,我想使用 for 循环对其进行迭代,并创建一个仅包含唯一单词的新列表。这类似于question asked previously,但我无法为列表中的列表

找到适合我的解决方案

例如嵌套列表如下:

ListofList = [['is', 'and', 'is'], ['so', 'he', 'his'], ['his', 'run']],

所需的输出将是一个列表:

List_Unique = [['is','and','so','he','his','run']]


我尝试了以下两种代码变体,但它们的输出都是重复列表:

unique_redundant = [] 
for i in redundant_search:
    redundant_i = [j for j in i if not i in unique_redundant]
    unique_redundant.append(redundant_i)
unique_redundant


unique_redundant = [] 
for list in redundant_search:
    for j in list:
        redundant_j = [i for i in j if not i in unique_redundant]
    unique_redundant.append(length_j)
unique_redundant

上述两个(不正确)变体的示例输出

(我在我的真实数据集上运行了代码,它在同一对单词的列表中给出了重复列表,尽管这不是实际的两个单词,只是一个示例):

List_Unique = [['is','and'],['is','and'],['is','and']]

【问题讨论】:

  • 你想要嵌套列表输出还是单个列表?
  • 我需要一个列表。因此,唯一搜索是在所有嵌套列表中查找唯一值,而不仅仅是在每个列表中
  • 好的,下面有答案,看看吧

标签: python list for-loop


【解决方案1】:

我建议以这种方式使用set() class union()

ListofList = [['is', 'and', 'is'], ['so', 'he', 'his'], ['his', 'run']]

set().union(*ListofList)
# => {'run', 'and', 'so', 'is', 'his', 'he'}

说明

它的工作原理如下:

test_set = set().union([1])
print(test_set)
# => {1}

列表前的星号运算符 (*ListofList) 解包列表:

lst = [[1], [2], [3]]
print(lst) #=> [[1], [2], [3]]
print(*lst) #=> [1] [2] [3]

【讨论】:

  • 这是唯一的pythonic anwer
【解决方案2】:

首先使用itertools.chain 展平列表,然后使用set 返回唯一元素并将其传递到列表中:

from itertools import chain

if __name__ == '__main__':
    print([{list(chain(*list_of_lists))}])

【讨论】:

    【解决方案3】:

    使用itertools.chain 使列表变平,使用dict.fromkeys 使唯一值保持有序:

    ListofList = [['is', 'and', 'is'], ['so', 'he', 'his'], ['his', 'run']]
    
    from itertools import chain
    List_Unique = [list(dict.fromkeys(chain.from_iterable(ListofList)))]
    

    【讨论】:

    • 我的真实数据集是一些 30k 嵌套列表,所以当我尝试该代码时,它会抛出错误 from_iterable() takes exactly one argument (31962 given) 任何想法?
    • @Brian 有错字,请检查更新
    【解决方案4】:

    只需在while的帮助下索引出嵌套列表并获取新列表while cnt<len(listoflist)中的所有值

    ListofList = [['is', 'and', 'is'], ['so', 'he', 'his'], ['his', 'run']]
    list_new=[]
    cnt=0
    while cnt<len(ListofList):
        for i in ListofList[cnt]:
            if i in list_new:
                continue
            else:
                list_new.append(i)
        cnt+=1
    print(list_new)
    

    输出

    ['is', 'and', 'so', 'he', 'his', 'run']
    

    【讨论】:

      【解决方案5】:
      flat_list = [item for sublist in ListofList for item in sublist]
      
      # use this if order should not change
      List_Unique = []
      for item in flat_list:
          if item not in List_Unique:
              List_Unique.append(item)
      
      
      # use this if order is not an issue
      # List_Unique = list(set(flat_list))
      

      【讨论】:

        【解决方案6】:

        你可以试试这个:

        ListofList = [['is', 'and', 'is'], ['so', 'he', 'his'], ['his', 'run']]
        uniqueItems = []
        for firstList in ListofList:
            for item in firstList:
                if item not in uniqueItems:
                    uniqueItems.append(item)
        print(uniqueItems)
        

        它使用一个嵌套的for循环来访问每个项目并检查它是否在uniqueItems中。

        【讨论】:

        • 这对我有用。谢谢
        【解决方案7】:

        使用基本的集合概念,集合由独特的元素组成

        lst = [['is', 'and', 'is'], ['so', 'he', 'his'], ['his', 'run']]
        new_list = []
        for x in lst:
            for y in set(x):
                new_list.append(y)
        print(list(set(new_list)))
        
        
        ['run', 'and', 'is', 'so', 'he', 'his']
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-03-26
          相关资源
          最近更新 更多