假设我有一个清单:
l = ['a', 'b', 'c', 'd', 'e', 'f', 'e']
Run Code Online (Sandbox Code Playgroud)
如您所见,重复索引4和6.我的问题是:查看列表中是否有重复内容的最有效方法是什么?
选项1:
output = len(set(l)) != len(l):
Run Code Online (Sandbox Code Playgroud)
如果输出为false,则其中有一个值不止一次.
选项2:
output = True
for i in l:
if l.count(i) > 1:
output = False
Run Code Online (Sandbox Code Playgroud)
如果输出为false,则其中有一个值不止一次.
问题:
什么是最这样做的有效途径?
如何计算这两个(或更多?)选项的O表示法?
谢谢!