简体   繁体   中英

Python - Not sure how to append tuples to dictionary value

Here is the dictionary:

{0.0: (1, 1.0),
 1.0: (121, 1.0),
 2.0: (253, 1.0),
 3.0: (68, 1.0),
 4.0: (460, 1.0),
 5.0: (147, 1.0),
 6.0: (33, 1.0),
 7.0: (127, 1.0),
 8.0: (171, 1.0),
 9.0: (36, 1.0),
 10.0: (559, 1.0)}

Now, what I'd like to do is append another tuple to a specific value.

{0.0: [(1, 1.0), (324, 1.0)],
 1.0: (121, 1.0),
 2.0: (253, 1.0),
 3.0: (68, 1.0),
 4.0: (460, 1.0),
 5.0: (147, 1.0),
 6.0: (33, 1.0),
 7.0: (127, 1.0),
 8.0: (171, 1.0),
 9.0: (36, 1.0),
 10.0: (559, 1.0)}

All of my attempts to instantiate a list and append to it just return with a None value.

Is there a clean way to complete this task?

[EDIT]: I appreciate your initial replies. The only issue I'm seeing now is that if I try to append another tuple to this list, it returns [None, tuple]

For easier handling you should turn every tuple into a list of tuples and then add values.

a = {0.0: (1, 1.0),
     1.0: (121, 1.0),
     2.0: (253, 1.0),
     3.0: (68, 1.0),
     4.0: (460, 1.0),
     5.0: (147, 1.0),
     6.0: (33, 1.0),
     7.0: (127, 1.0),
     8.0: (171, 1.0),
     9.0: (36, 1.0),
     10.0: (559, 1.0)}

for key in a:
    a[key] = [a[key]]

expand_key = 0.0
expand_value = (324, 1.0)

a[expand_key].append(expand_value)

print(a[expand_key], a[7.0])

Output: [(1, 1.0), (324, 1.0)] [(127, 1.0)]

Where dct is your dictionary, tup is the tuple you want to add and key is the key which you want to add it to

def add_tuple(dct, tup, key):
    if isinstance(dct[key], list):
        dct[key] = dct[key].append(tup)
        return
    dct[key] = [dct[key], tup]

The first thing you would want to do is turn each value into a list, or even better reconstruct your dictionary to have these tuples inserted as an element in the list.

To convert values such that they become elements in a list:

for key in ex_dict:
  ex_dict[key]=list(ex_dict[key])

Then you can simply add new elements to the key by doing

ex_dict[key].append(tup) # where tup is the tuple you want to add to the list

Note that you don't want to assign an append function call to anything, as that returns a None. Just executing the above line will update the value for that key.

An easier and more pythonic implementation would be to construct your dictionary using the standard library's dictionary methods, here's a generic demonstration:

ex_dict = {}
for key,value in zip(keys,values):
  ex_dict.setdefault(key,[]).append(value)

What setdefault does is it will look for the key in the dictionary, and if the key is not found, will return a default value, which in this case is set to an empty list.

Another implementation, directly suited to your use case is to use the collection module's defaultdict data structure as follows:

from collections import defaultdict
ex_dict = defaultdict(list)
for key,value in zip(keys,values):
  ex_dict[key].append(value)

Then when you want to add new elements to existing keys:

for key,value in zip(keys,values):
  ex_dict[key].append(value)

Note that the same piece of code (looping over keys and values) can be used to create keys and values as well as append values to existing keys.

Hope this made sense.

dict1 = {0.0: (1, 1.0),
     1.0: [(121, 1.0), (253, 1.0)],
     2.0: (253, 1.0),
     3.0: (68, 1.0),
     4.0: (460, 1.0),
     5.0: (147, 1.0),
     6.0: (33, 1.0),
     7.0: (127, 1.0),
     8.0: (171, 1.0),
     9.0: (36, 1.0),
     10.0: (559, 1.0)}

def add_tuple(mdict, mkey, newtuple):
if isinstance(mdict[mkey], list):
    mdict[mkey].append(newtuple)
elif isinstance(mdict[mkey], tuple):
    mdict[mkey] = [mdict[mkey], newtuple]

print(dict1[5.0])
add_tuple(dict1, 5.0, (55, 5.0))
print(dict1[5.0])
add_tuple(dict1, 5.0, (77, 7.0))
print(dict1[5.0])

this method works fine. No need to convert anything.

Output:

(147, 1.0)

[(147, 1.0), (55, 5.0)]

[(147, 1.0), (55, 5.0), (77, 7.0)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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