简体   繁体   中英

Building a dictionary with lists as values using list comprehension

I'm trying to simplify the following code using , in which I want to build a dictionary of lists from a list.来简化以下代码,其中我想从列表中构建列表字典。

In this example, I take the mod (or any other function) of each item as the key. If the key doesn't exist in the dictionary, a new list is created and the item is appended to the list, otherwise the item is directly appended.

def get_key(val):
    return val % 3 # Can be something else, it doesn't matter


dict = {}
data = [i for i in range(10)]
for item in data:
    key = get_key(item)
    if key in dict:
        dict[key].append(item)
    else:
        dict[key] = [item]
print(dict)

Expected output

{0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]}

To simplify the above code, I have tried this ugly approach:

for item in data:
    key = get_key(item)
    dict[key] = [item] if key not in dict else (dict[key], dict[key].append(item))[0]

However, how to achieve the same thing without the for-loop, using ?来实现相同的目标?

I could think of something like this, but it's not able to append value into a pre-existing list.

{get_key(item):[item] for item in data}

Related posts:

You are close. below is my approach for using dict comprehension

data = list(range(0,10))
mod= 3
{i:data[i::mod] for i in range(mod)}

Out

{0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]}

Create a list of list of the values, with the first element of each sublist being the key which is [n] + , then use that to create a dictionary.

m = [[n] + [i[1] for i in [(x%3,x) for x in range(10)]if i[0] == n] for n in range(3)]

dictt = {i[0]:i[1:] for i in m}
print(dictt)

>>> {0: [0, 3, 6, 9], 1: [1, 4, 7], 2: [2, 5, 8]}

All on one line

dictt = {i[0]:i[1:] for i in [[n] + [i[1] for i in [(x%3,x) for x in range(10)]if i[0] == n] for n in range(3)]}

You don't really need list/dict comprehension here. Just use a normal loop with defaultdict :

out = collections.defaultdict(list)
for i in range(10):
    out[i % 3].append(i)

Try default dict ,

from collections import defaultdict

d = defaultdict(list)
for item in range(10): 
    d[item % 3].append(item)

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