我正在尝试创建一个程序,该程序按字母顺序排列单词并删除重复项。例如,用环游世界,环游世界,它应该变成 ['around', 'the', 'world']。但是,我得到的输出是 ['around', 'around', 'the', 'the', 'world,', 'world.']
我不允许使用字典或集合来轻松删除重复项。如何删除重复的单词而不使用它们?
您可以做的是制作另一个列表来存储您的答案。迭代第一个列表,如果答案列表中不存在该单词,请将其附加到答案。
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.']
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句