列表中没有唯一字典? [英] Unique dictionaries out of a list of lists?
问题描述
我有一个名为matrix
的列表,其中包含一些行.每个row
都包含一些词典,每个词典可以包含多于一行.
I have a list called matrix
which contains some rows. Each row
contains some dictionaries, and each dictionary could be contained in more than one row.
我想生成一个名为dictionaries
的列表,该列表包含矩阵中的所有词典,但无重复.我已经有一个解决方案,但是我想使用理解力.
I want to generate a list called dictionaries
which contains all the dictionaries in the matrix, but without duplicates. I already have a solution, but I would like to use comprehension.
row1 = [{'NODE':1}, {'NODE':2}, {'NODE':3}]
row2 = [{'NODE':3}, {'NODE':4}, {'NODE':5}]
row3 = [{'NODE':4}, {'NODE':6}, {'NODE':7}]
matrix = [row1, row2, row3]
dictionaries = []
for row in matrix:
for dictionary in row:
items.append(dictionary) if dictionary not in dictionaries else None
print dictionaries
[{'NODE':1}, {'NODE':2}, {'NODE':3}, {'NODE':4}, {'NODE':5}, {'NODE':6}, {'NODE':7}]
我想要以下内容,但是它不起作用,因为在创建列表时我无法要求检查列表:
I would like something like the following but it doesn't work since I cannot ask to check a list while I'm creating it:
dictionaries = [dictionary for row in matrix for dictionary in row if dictionary not in dictionaries]
字典键和值是不可变的原始对象,例如字符串和整数.
The dictionary keys and values are primitive immutable objects like strings and integers.
推荐答案
You could use a list comprehension, but depending on your Python version, using an collections.OrderedDict
object with a generator expression to flatten the matrix would actually be more efficient.
当您的值不可散列从而无法存储在集合或字典中时,您必须首先使用创建不可变的表示形式,这样我们才能将该表示形式存储在集合中或字典来有效地跟踪唯一性.
When your values are not hashable and thus can't be stored in a set or dictionary, you'll have to use first create an immutable representation, so we can store that representation in a set or dictionary to efficiently track uniqueness.
对于具有所有键和值均不变的平面结构的字典,只需使用tuple(sorted(d.items()))
.这样会产生所有(key, value)
对(也为元组)的元组,并按排序顺序以避免字典顺序问题.
For dictionaries that are flat structures with all keys and values immutable, just use tuple(sorted(d.items()))
. This produces a tuple of all (key, value)
pairs (also tuples), in sorted order to avoid dictionary order issues.
在Python 3.5及更高版本上,使用OrderedDict()
将不可变键映射到原始字典:
On Python 3.5 and up, use an OrderedDict()
that maps the immutable keys to original dictionaries:
from collections import OrderedDict
key = lambda d: tuple(sorted(d.items()))
dictionaries = list(OrderedDict((key(v), v) for row in matrix for v in row).values())
在Python 3.4及更早版本上,OrderedDict
速度很慢,您最好对Python 3.4及以下版本使用单独的set方法:
On Python 3.4 and earlier, OrderedDict
is slow and you'd be beter of using a separate set approach for Python 3.4 and below:
key = lambda d: tuple(sorted(d.items()))
seen = set()
seen_add = seen.add
dictionaries = [
v for row in matrix
for k, v in ((key(v), v) for v in row)
if not (k in seen or seen_add(k))]
使用您的输入数据和OrderedDict
的快速演示:
Quick demo using your input data and an OrderedDict
:
>>> from collections import OrderedDict
>>> row1 = [{'NODE':1}, {'NODE':2}, {'NODE':3}]
>>> row2 = [{'NODE':3}, {'NODE':4}, {'NODE':5}]
>>> row3 = [{'NODE':4}, {'NODE':6}, {'NODE':7}]
>>> matrix = [row1, row2, row3]
>>> key = lambda d: tuple(sorted(d.items()))
>>> list(OrderedDict((key(v), v) for row in matrix for v in row).values())
[{'NODE': 1}, {'NODE': 2}, {'NODE': 3}, {'NODE': 4}, {'NODE': 5}, {'NODE': 6}, {'NODE': 7}]
这篇关于列表中没有唯一字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!