[英]"TypeError: '>' not supported between instances of 'int' and 'NoneType'"
[英]'<' not supported between instances of 'int' and 'NoneType'
我是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'
嘗試將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)
在此處更多地使用append
而不是extend
(附加是將一個項目附加到列表,而擴展是將一個列表添加到另一個列表)。
input
接收一個參數,更多信息在這里
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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.