我想遍历一个大的二维列表:
authors = [["Bob", "Lisa"], ["Alice", "Bob"], ["Molly", "Jim"], ... ]
并获得一个列表,其中包含作者中出现的所有名称。
当我遍历列表时,我需要一个容器来存储我已经看过的名称,我想知道我应该使用列表还是字典:
有一个清单:
seen = []
for author_list in authors:
for author in author_list:
if not author in seen:
seen.append(author)
result = seen
用字典:
seen = {}
for author_list in authors:
for author in author_list:
if not author in seen:
seen[author] = True
result = seen.keys()
哪个更快?还是有更好的解决方案?