小编Abr*_*amB的帖子

查找列表中是否重复某些内容的最有效方法是什么?

假设我有一个清单:

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,则其中有一个值不止一次.

问题:

  1. 什么是这样做的有效途径?

  2. 如何计算这两个(或更多?)选项的O表示法?

谢谢!

python list python-2.7

2
推荐指数
1
解决办法
599
查看次数

标签 统计

list ×1

python ×1

python-2.7 ×1