我是python的初学者,我正在编写代码以从列表中删除所有重复的数字。
首先我尝试使用for循环,但出现类似错误
lis=[]
lis2=[]
n=print("enter number of items you want in the list \n")
i=0
while (i<n):
a=input("enter element number ",i+1)
lis.extend(a)
i=i+1
for j in lis:
if j not in lis2:
lis2.extend(j)
print(" \n list after removing duplicates \n",lis2)
input()
错误:'<' not supported between instances of 'int' and 'NoneType'
答案 0 :(得分:0)
尝试将n=print("enter number of items you want in the list \n")
更改为:
n_ = input("enter number of items you want in the list \n")
n = int(n_)
应用上述更改后,我还注意到了一些错误,这些错误在经过一些修复后变为:
lis=[]
lis2=[]
n_=input("enter number of items you want in the list \n")
n = int(n_)
i=0
while (i<n):
a=input("enter element number " + str(i+1))
lis += [a]
i=i+1
for j in lis:
if j not in lis2:
lis2 += [j]
print(" \n list after removing duplicates \n",lis2)
答案 1 :(得分:0)
在here中更多地使用append
代替extend
(追加将一个项目添加到列表,而扩展将一个项目添加到另一个列表)。
input
收到一个参数,here中收到更多参数
lis=[]
lis2=[]
n=input("enter number of items you want in the list \n")
i=0
while (i<n):
a=input("enter element number " + str(i + 1))
lis.append(a)
i=i+1
for j in lis:
if j not in lis2:
lis2.append(j)
print(" \n list after removing duplicates \n",lis2)