How do you add multiple tuples(lists, whatever) to a single dictionary key without merging them?

冷暖自知 提交于 2019-12-22 11:06:37

问题


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.

For instance say the dictionary = {'Key1':(1.000,2.003,3.0029)}

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))} (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!

Edit: 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.


回答1:


Just map your key to a list, and append tuples to the list.

d = {'Key1': [(1.000,2.003,3.0029)]}

Then later..

d['Key1'].append((2.3232,13.5232,1325.123))

Now you have:

{'Key1': [(1.0, 2.003, 3.0029), (2.3232, 13.5232, 1325.123)]}



回答2:


Use defaultdict and always use append and this will be seemless.

from collections import defaultdict

x = defaultdict(list)
x['Key1'].append((1.000,2.003,3.0029))



回答3:


A dictionary value can't contain two tuples just by themselves. Each dictionary key maps to a single value, so the only way you can have two separate tuples associated with that key is for them to be themselves contained within a tuple or list: {'Key1':[(1.000,2.003,3.0029),(2.3232,13.5232,1325.123)]} - note the extra pair of square brackets.

One way of doing this would be to get the current value associated with your key, and append it to a list before setting the new list back to that key. But if there's a possibility you'll need that for any key, you should do it for all keys right at the start, otherwise you'll get into all sorts of difficulties working out what level you're at.




回答4:


Instead of:

{'Key1':(1.000,2.003,3.0029)}

What you want is:

{'Key1':[(1.000,2.003,3.0029)]}

When you add another tuple, you'll get:

{'Key1':[(1.000,2.003,3.0029), (2.3232,13.5232,1325.123)]}



回答5:


I think your questions is somewhat badly formulated. You want to:

  1. Associate a tuple to a key, if the key is not in the dictionary
  2. Replace the tuple with a list of two tuples, if the key is in the dictionary and points to a tuple
  3. Append a tuple to the list of tuples associated to a key

This could be achieved with the following code:

def insertTuple(d, k, tup):
    if k not in d:
        d[k] = tup
    elif type(d[k]) == tuple:
        d[k] = [ d[k], tup ]
    else:
        d[k].append(tup)



回答6:


As we know tuples are not mutuable. Instead of using a tuple of tuples. Use list of tuples this will work.

first create a list of tuples and then append it to a dictionay key.\

dictionary = {}
listoftuples = []//create a tuples of list
dictionary[key].append(listoftuples)//appending it to a dictionary key 


来源:https://stackoverflow.com/questions/10301589/how-do-you-add-multiple-tupleslists-whatever-to-a-single-dictionary-key-witho

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!