如何在python中过滤列表理解中的重复值而不使用集合
我必须在列表中过滤重复的值如何在python中过滤列表理解中的重复值而不使用集合,python,list-comprehension,Python,List Comprehension,我必须在列表中过滤重复的值 with open(file_name, 'rU') as input_file: result = [unique for unique in [process(line) for line in input_file] if **unique_not_in_generated_list**] 在生成的列表中可以使用什么表达式来代替唯一的 Python2.7可以像这样使用 from itertools import group
with open(file_name, 'rU') as input_file:
result = [unique for unique in [process(line) for line in input_file] if **unique_not_in_generated_list**]
在生成的列表中可以使用什么表达式来代替唯一的
Python2.7可以像这样使用
from itertools import groupby
print [unique for unique, _ in groupby(sorted(process(line) for line in input_file))]
注意:这不维护数据的顺序,但保证在不使用集合的情况下生成唯一的项。只需使用生成器表达式和
set()
:
如果需要保留排序,仍然可以使用set。为了便于阅读,我还牺牲了对列表的理解:
result = list()
seen = set()
with open(file_name, 'rU') as input_file:
for line in input_file:
unique = process(line)
if unique not in seen:
seen.add(unique)
result.append(unique)
如果你坚持使用列表理解和保持秩序,你可以通过使用一个一次性的None
s列表来做到这一点,但是这种方法违背了目的,是O(N^2)
这里有一个单行程序,可以在保持行有序的同时删除重复项。使用
为什么厌恶集合?他不喜欢集合,可能是因为他不想改变顺序。排序un可以做到这一点。虽然问题中没有明确提到/@strustmaster,但排序不会杀死行排序:(@inspectorG4dget会的,但OP没有特别提到它:(@thefourtheye是对的,但这只是不使用
set
的一个解决办法;可能效率更低。OrderedDict.fromkeys可能适用于OP,如我的回答中所示。
result = list()
seen = set()
with open(file_name, 'rU') as input_file:
for line in input_file:
unique = process(line)
if unique not in seen:
seen.add(unique)
result.append(unique)
result = list()
with open(file_name, 'rU') as input_file:
[result.append(x) for process(x) in input_file if process(x) not in result]
result = list(OrderedDict.fromkeys(process(line) for line in input_file))