我有一个列表(例如:[1, 2, 2, 3, 4, 3]
),我只需要保留列表中任何元素的第一个出现位置(例如:列表应该成为[1, 2 ,3, 4]
)。我知道我可以做这样的事情:
badList = [1, 2, 2, 3, 4, 3]
goodList = []
for element in badList:
if element in goodList:
continue
goodList.append(element)
print (goodList)
然而,这是一个非常混乱的解决方案,我希望有一种更优雅的方式。
答案 0 :(得分:2)
from collections import OrderedDict
list(OrderedDict.fromkeys(badList))
[1, 2, 3, 4]
答案 1 :(得分:0)
只需转换为集合然后返回列表:
Could not locate Gemfile
ERROR: Service 'webapp_setup' failed to build: The command '/bin/sh -c bundle install' returned a non-zero code: 10