简体   繁体   English

如何将带有元组的元组列表转换为字典?

[英]How do I convert a list of tuples with sets to a dictionary?

I have this dataset. 我有这个数据集。 I want to update the MySQL table. 我想更新MySQL表。 I can do it in the current form but I thought a conversion to dictionary will shrink the list to be updated. 我可以在当前表单中执行此操作,但我认为转换为字典会缩小列表以进行更新。

My dataset : 我的数据集:

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]

Desired output : 期望的输出:

A dictionary : 一本字典 :

output = {'set(['NY'])':121,198,676, 'set(['CA', 'NY'])':132,89}

You must use a frozenset for the key. 必须为密钥使用冻结集。 There is no guarantee that a set with the same elements will always be turned into the same repr or tuple as sets are unordered. 无法保证具有相同元素的集合将始终转换为与集合无序相同的reprtuple Unless you sort the set elements first of course, but that seems wasteful 除非你首先对设定元素进行排序,否则这看起来很浪费

from collections import defaultdict

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]
output = defaultdict(list)
for value, key in dataset:
    output[frozenset(key)].append(value)

or using a sorted tuple 或使用排序的元组

from collections import defaultdict

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]
output = defaultdict(list)
for value, key in dataset:
    output[tuple(sorted(key))].append(value)

Random example to illustrate this 随机举例来说明这一点

>>> s,t = set([736, 9753, 7126, 7907, 3350]), set([3350, 7907, 7126, 9753, 736])
>>> s == t
True
>>> tuple(s) == tuple(t)
False
>>> frozenset(s) == frozenset(t)
True
>>> hash(tuple(s)) == hash(tuple(t))
False
>>> hash(frozenset(s)) == hash(frozenset(t))
True

I don't think you can have a set as a dictionary key, so maybe a tuple? 我不认为你可以有一set作为字典键,所以也许一个元组?

from collections import defaultdict

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]
output = defaultdict(list)
for value, key in dataset:
    output[tuple(key)].append(value)
    # or output[str(key)].append(value) if you want a string as the key

Try this: 尝试这个:

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]
from collections import defaultdict
d = defaultdict(list)

for val, key in dataset:
    d[repr(key)].append(int(val))

d
> {"set(['NY', 'CA'])": [132, 89], "set(['NY'])": [121, 198, 676]}

Here is an alternative to defaultdict : 这是defaultdict的替代方法:

dataset = [('121', set(['NY'])), ('132', set(['CA', 'NY'])), ('198', set(['NY'])), ('676', set(['NY'])), ('89', set(['NY', 'CA']))]    

output = {}
for value, key in dataset:
   output.setdefault(frozenset(key), []).append(value)

Result: 结果:

>>> output
{frozenset(['NY', 'CA']): ['132', '89'], frozenset(['NY']): ['121', '198', '676']}

I prefer using setdefault() over defaultdict here because of the following behavior: 由于以下行为,我更喜欢在defaultdict使用setdefault()

>>> output = defaultdict(list, {frozenset(['NY', 'CA']): ['132', '89'], frozenset(['NY']): ['121', '198', '676']})
>>> output[frozenset(['FL'])]    # instead of a key error, this modifies output
[]
>>> output
defaultdict(<type 'list'>, {frozenset(['NY', 'CA']): ['132', '89'], frozenset(['FL']): [], frozenset(['NY']): ['121', '198', '676']})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM