Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在不使用字典或集合的情况下删除列表中的重复项?_Python 3.x - Fatal编程技术网

Python 3.x 如何在不使用字典或集合的情况下删除列表中的重复项?

Python 3.x 如何在不使用字典或集合的情况下删除列表中的重复项?,python-3.x,Python 3.x,我正在尝试创建一个程序,该程序按字母顺序对单词进行排序,并删除重复的单词。例如,与环游世界,环游世界,它应该成为['Around','the','world']。然而,我得到的输出是['around','around','the','the','world','world.] 我不允许使用字典或set,这样可以很容易地删除重复项。如何删除重复的单词而不使用它们?您可以做的是制作另一个列表来存储您的答案。如果单词不在答案列表中,请在第一个列表上迭代,将其附加到答案 lst=['around', '

我正在尝试创建一个程序,该程序按字母顺序对单词进行排序,并删除重复的单词。例如,与环游世界,环游世界,它应该成为['Around','the','world']。然而,我得到的输出是['around','around','the','the','world','world.]


我不允许使用字典或set,这样可以很容易地删除重复项。如何删除重复的单词而不使用它们?

您可以做的是制作另一个列表来存储您的答案。如果单词不在答案列表中,请在第一个列表上迭代,将其附加到答案

lst=['around', 'around', 'the', 'the', 'world.', 'world.']
lst2=[] ##answer list

for word in lst:

  if word not in lst2:

  lst2.append(word)

print(lst2)
输出

['around', 'the', 'world.']

最简单的方法是使用set。若您不应该使用set,那个么可以创建另一个新列表并使用append

listt = ['around','the','world','around','the','world']
listt2 = []

for i in listt:
     if i not in listt2:
     listt2.append(i)
print(listt2)