Python 如何从字符串/混合列表中删除重复项

Python 如何从字符串/混合列表中删除重复项,python,Python,我正在尝试打印第二份列表,而不打印第二份副本。我想我可以使用x.remove(list),但它不起作用。有什么建议吗 x = [] #initiate empty string counter = {} while list != "DONE": list = input() #keep asking for input for i in x: if not i in counter: x

我正在尝试打印第二份列表,而不打印第二份副本。我想我可以使用x.remove(list),但它不起作用。有什么建议吗

x = [] #initiate empty string
    counter = {}

    while list != "DONE":
        list = input() #keep asking for input

        for i in x:
            if not i in counter:
                x.append(list)
            else:
                x.remove(list)

如果您不希望列表中出现重复项,可以使用:

In [38]: l1 = [1,2,1,2,1,2]

In [39]: l2= list(set(l1))

In [40]: l2
Out[40]: [1, 2]

我认为这就是你想要实现的目标:

x = []
while True:
    data = input()
    if data.lower() == "done":
        break
    if data not in x:
        x.append(data)
注意使用
while True
break
以避免有两个
输入调用

或者,使用:

这将忽略添加重复项的尝试

如果您确实希望允许用户通过第二次输入从
x
中删除项目,您可以添加
else
并使用
remove
(用于列表)或
discard
(用于集合),例如:


当你说它不起作用时,你需要更具体一些。你以为会发生什么?到底发生了什么?这里有几个问题。这是错误的。您正在使用
list
作为变量名。您在定义它之前访问它(这只适用于
list
恰好是一个内置名称)。您使用的是一个字典
计数器
,它始终保持为空。你的问题很让人困惑。什么是“第二名单”?什么是“第二次复制”?什么“不起作用”?请用示例输入和输出解释应该发生什么。谢谢。如果用户再次输入项目,是否确实要删除它们?还是不加第二次?您可以通过将
x
a
set
简化此操作。
x = set()
while True:
    data = input()
    if data.lower() == "done":
        break
    x.add(data)
x = list(x)
x = set() # or []
while True:
    data = input()
    if data.lower() == "done":
        break
    if data in x:
        x.discard(data) # or .remove(data)
    else:
        x.add(data) # or .append(data)