【问题标题】:How can I delete duplicate numbers in a list (Python)?如何删除列表中的重复数字(Python)?
【发布时间】:2021-01-06 08:38:16
【问题描述】:

我有一个清单:

words = ['1,2,3,4',
         '5,7,8,4',
         '1,2,3,9']

我的目标是从单词中创建一个新列表,但没有重复的数字。

这是我想要的一个例子!

new_list=[1,2,3,4,5,7,8,9]

我做到了:


words = ['1,2,3,4',
         '5,7,8,4',
         '1,2,3,9']

new_list = []
for i in words :
    if i not in new_list:
        new_list.append(i)
print(new_list)

但我的列表中又出现了相同的数字:

['1,2,3,4', '5,7,8,4', '1,2,3,9']

编辑:

我想做同样的事情,但用真实的话,喜欢它:


[" apple is not good",  "mongo is delicious",  banana is very good"] 

而我的新列表必须是这些短语中的每一个单词都是独一无二的。 这是我想要的结果示例:

["apple,is,not,good,mongo,delicious,banana,very"]

如您所见,我只从列表中的短语中获得了独特的单词。

【问题讨论】:

  • 你有单词,每个单词都是独一无二的。你可能想检查字符吗?
  • 是的,我想检查我的完整列表中的字符
  • 你说 new_list=[1,2,3,4,5,7,8,9] 是你想要的。它是否正确?这是你想要的,还是 new_list=['1','2','3','4','5','7','8','9']?很大的区别。第一个是数字列表。第二个是字符串列表。
  • 为什么您的结果中缺少banana
  • 对不起,这是我的错误

标签: python list duplicates


【解决方案1】:

你可以将words的元素与itertools chain模块组合起来,然后用set()方法消除所有重复:

import itertools
words = ['1,2,3,4','5,7,8,4','1,2,3,9']
c = list(set(itertools.chain(words[0].replace(',',''),words[1].replace(',',''),words[2].replace(',',''))))
new_words = [int(x) for x in c]
new_words.sort()
print(new_words)

结果

[1, 2, 3, 4, 5, 7, 8, 9]

为了更新

import itertools
words = ["apple is not good",  "mongo is delicious",  "banana is very good"] 
new_words = list(set(itertools.chain(words[0].split(' '), words[1].split(' '), words[2].split(' '))))
print(new_words)

结果

['not', 'mongo', 'is', 'apple', 'good', 'very', 'delicious', 'banana']

【讨论】:

    【解决方案2】:
    You need to use extend,  not append. And also split each line. Convert to "set" to remove duplicate items
    words = ['1,2,3,4',
                 '5,7,8,4',
                 '1,2,3,9']
    
    new_list = []
    for i in words :
            new_list.extend(i.split(','))
    new_list = list(set(new_list))
    new_list.sort()
    print(new_list)
    

    保持元素的顺序

        words=['apple,banana,orange', 'apple, mango']
    
        new_list = []
        for i in words :
                new_list.extend(i.split(','))
        result =[]
        for i in new_list:
            if i not in result:
                result.append(i)
        
        print(result)
    

    【讨论】:

    • ty ,但你的第二个例子不起作用,我猜我的最终列表中有一个苹果的副本
    • 它有效,我只是输出了错误的列表。它必须是打印(结果)
    • 没错,我做错了,我想说如果你得到了它就没有用:['apple banana orange' , 'apple mango']
    【解决方案3】:
    words = ['1,2,3,4',
             '5,7,8,4',
             '1,2,3,9']
    
    new_list = []
    for w in words :
        for i in map(int, w.split(',')):
            if i not in new_list:
                new_list.append(i)
    print(new_list)
    

    诀窍是从列表中的每个单词中提取数字。每个word 都是由逗号分隔的数字串。因此,w.split(',') 将每个逗号分隔数字字符串拆分为一个列表。 map() 函数将int() 方法应用于每个数字字符串,将其转换为数值。如果该值不存在,则将该值添加到 new_list 中。

    此解决方案还可以处理大于 9 的数字。

    另外,作为一个例子来帮助理解map

    "1,2,3,4".split(",") --> ["1", "2", "3", "4"]
    

    map(int, ["1", "2", "3", "4"]) --> [1, 2, 3, 4]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-01
      • 2023-01-08
      • 2011-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多