簡體   English   中英

在'int'和'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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM