我将收到一个具有这种结构的json
{'compare': [["A","B","C"], ["A","D","C"], ["E","A"]...}
我将比较每个数组,并且我想创建一个只有唯一项的新列表,例如
{"unique":["B","D","E"]}
合并所有列表,计算元素,只保留唯一的列表。 下面使用列表的列表,而不是字典(字典没有带来任何值)。 使用内置包是最快和最Python的:
import collections,itertools
lst = [["A","B","C"], ["A","D","C"], ["E","A"]]
# flatten lists and count elements
c = collections.Counter(itertools.chain.from_iterable(lst))
# filter, keep only elements counted once
result = [k for k,v in c.items() if v==1]
>>> result
['B', 'E', 'D']