目標は、すべての数字が1回しか表示されないリストを作成することです。助けてもらえますか:)
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
tempList = myList[:]
for i in myList:
for j in tempList:
if (myList[i] == tempList[j]):
del myList[i]
print("The list with unique elements only:")
print(myList)
あなたはただ使うことができますset
:
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
print(list(set(myList)))
出力:
[1, 2, 4, 6, 9]
ループを使用する場合は、次を試してください。
myList = [1, 2, 4, 4, 1, 4, 2, 6, 2, 9]
newList = []
for i in myList:
if i not in newList:
newList.append(i)
print(newList)
それはまた出力します:
[1, 2, 4, 6, 9]
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加