Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
删除python列表中的重复编号_Python_List_Duplicates - Fatal编程技术网

删除python列表中的重复编号

删除python列表中的重复编号,python,list,duplicates,Python,List,Duplicates,我有一份清单: 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

我有一份清单:

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"]
正如你所看到的,我从列表中的短语中只找到了唯一的单词。我不知道该怎么做,我有点迷路了

谢谢

保持元素的顺序

    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)
诀窍是从列表中的每个单词中提取数字。每个
单词都是由逗号分隔的一系列数字组成的字符串。因此,
w.split(',')
将每个逗号分隔的数字字符串拆分为一个列表。
map()
函数将
int()
方法应用于将其转换为数值的每个数字字符串。然后,如果该值尚未存在,则将其添加到新的_列表中

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

另外,作为帮助理解
地图
的示例:

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


快乐编码

您可以将
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]是你想要的。这是正确的吗?这就是你想要的,还是新的列表=['1','2','3','4','5','7','8','9']?差别很大。第一个是数字列表。第二个是字符串列表。为什么您的结果中缺少了
banana
?很抱歉,这是mety的错误,但是您的第二个示例不起作用,我想我的最终列表中有一个苹果的副本。它起作用了,我只是输出了错误的列表。它一定是打印(结果)正确,我做得不好,我想说,如果你得到它,它不工作:[“苹果香蕉橙”,“苹果芒果”]
"1,2,3,4".split(",") --> ["1", "2", "3", "4"]
map(int, ["1", "2", "3", "4"]) --> [1, 2, 3, 4]
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']