You really want a set
. Sets are faster than lists because they can only contain unique elements, which allows them to be implemented as hash tables. Hash tables allow membership testing (if element in my_set
) in O(1)
time. This contrasts with lists, where the only way to check if an element is in the list is to check every element of the list in turn (in O(n)
time.)
。 两者均允许<代码>O(1) 成员测试。 差异是,<条码>只有钥匙,而<条码>则有条码>。 具有关键和价值(在申请中不需要额外的间接费用)。
Using a set
, and replacing the nested for loop with an itertools.chain()
to flatten the 2D list to a 1D list:
import itertools
seen = set()
for author in itertools.chain(*authors):
seen.add(author)
Or shorter:
import itertools
seen = set( itertools.chain(*authors) )
Edit (thanks, @jamylak)
import itertools
seen = set( itertools.chain.from_iterable(authors) )
名单例:
>>> a = [[1,2],[1,2],[1,2],[3,4]]
>>> set ( itertools.chain(*a) )
set([1, 2, 3, 4])
P.S.:如果你不想找到所有独特的作者,就希望。 页: 1 反代码>,特殊种类的字典选择计算。
这里的例子有:
>>> a = "DEADBEEF CAFEBABE"
>>> import collections
>>> collections.Counter(a)
Counter({ E : 5, A : 3, B : 3, D : 2, F : 2, : 1, C : 1})