如何在不合并的情况下将多个元组(列表等)添加到单个字典键中? [英] How do you add multiple tuples(lists, whatever) to a single dictionary key without merging them?
问题描述
我一直在尝试找出如何将包含多个值的多个元组添加到字典中的单个键.但是到目前为止没有成功.我可以将值添加到元组或列表中,但是我不知道如何添加元组,以便键现在具有2个包含值的元组,而不是一个包含所有值的元组.
I've been trying to figure out how to add multiple tuples that contain multiple values to to a single key in a dictionary. But with no success so far. I can add the values to a tuple or list, but I can't figure out how to add a tuple so that the key will now have 2 tuples containing values, as opposed to one tuple with all of them.
例如说字典= {'Key1':(1.000,2.003,3.0029)}
,我想添加(2.3232,13.5232,1325.123)
,以便最终得到:
and I want to add (2.3232,13.5232,1325.123)
so that I end up with:
dictionary = {'Key1':((1.000,2.003,3.0029),(2.3232,13.5232,1325.123))}
(忘了一组括号!)
dictionary = {'Key1':((1.000,2.003,3.0029),(2.3232,13.5232,1325.123))}
(forgot a set of brackets!)
如果有人知道如何做到这一点,我将不胜感激,因为它现在真的开始使我烦恼.
If someone knows how this can be done I'd appreciate the help as it's really starting to annoy me now.
谢谢!
谢谢大家!具有讽刺意味的是,我尝试过这样做,只是当时我试图使值成为多个列表而不是多个元组.解决方案是将元组封装在列表中.讽刺的是啊.
Thanks everyone! Ironic that I tried that except at the time I was trying to make the value multiple lists instead of multiple tuples; when the solution was to just enclose the tuples in a list. Ah the irony.
推荐答案
只需将您的密钥映射到列表,然后将元组附加到列表中即可.
Just map your key to a list, and append tuples to the list.
d = {'Key1': [(1.000,2.003,3.0029)]}
然后再..
d['Key1'].append((2.3232,13.5232,1325.123))
现在您拥有:
{'Key1': [(1.0, 2.003, 3.0029), (2.3232, 13.5232, 1325.123)]}
这篇关于如何在不合并的情况下将多个元组(列表等)添加到单个字典键中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!