TransWikia.com

How to convert three lists to dicts where keys are taken from a Cartesian product of the first two lists and the values are from the third list

Stack Overflow Asked by Anis Boudieb on December 11, 2021

I have three lists:

W = ['Anis', 'James', 'Arthur']
C = ['City1', 'City2', 'City3']
Cost = [53, 27, 13, 80, 47, 67, 53, 73, 47]

I want these results in this format:

{('Anis', 'City1'): 53}
{('Anis', 'City2'): 27}
{('Anis', 'City3'): 13}
{('James', 'City1'): 80}
{('James', 'City2'): 47}
{('James', 'City3'): 67}
{('Arthur', 'City1'): 53}
{('Arthur', 'City2'): 73}
{('Arthur', 'City3'): 47}

I have tried this

`for i in range(n1):
    for j in range(n1):
        for c in range(n2):
            combinations, mt = multidict({(W[i], C[j]): Cost[c]})
        print(mt)`

I got this instead:

{('Anis', 'City1'): 47}
{('Anis', 'City2'): 47}
{('Anis', 'City3'): 47}
{('James', 'City1'): 47}
{('James', 'City2'): 47}
{('James', 'City3'): 47}
{('Arthur', 'City1'): 47}
{('Arthur', 'City2'): 47}
{('Arthur', 'City3'): 47}

The cost values is not incrementing in the for loop (I am using gurobi optimizer library).

3 Answers

I have two ways in which you can do it. In the end you get your decired dicts but in a list containing the dicts.

name_place=[]
for name in W:
    for city in C:
        name_place.append((name, city))
 
final_list=[]        
for i, value in enumerate(Cost):
    final_list.append({name_place[i]:value})

print(final_list)

Alternatively for a little more compact code you can use listcomprehension:

print([{[(name,city) for name in W for city in C][i]: value} for i, value in enumerate(Cost)])

Answered by Alex S on December 11, 2021

Just another way, using an iterator:

cost = iter(Cost)
for w in W:
    for c in C:
        print({(w, c): next(cost)})

Output:

{('Anis', 'City1'): 53}
{('Anis', 'City2'): 27}
{('Anis', 'City3'): 13}
{('James', 'City1'): 80}
{('James', 'City2'): 47}
{('James', 'City3'): 67}
{('Arthur', 'City1'): 53}
{('Arthur', 'City2'): 73}
{('Arthur', 'City3'): 47}

Answered by Kelly Bundy on December 11, 2021

This can be done using a combination of itertools.product (every combination of values) for W and C, and then zip for pairs of corresponding values between the resulting tuples and your Cost list:

import itertools

W = ['Anis', 'James', 'Arthur']
C = ['City1', 'City2', 'City3']
Cost = [53, 27, 13, 80, 47, 67, 53, 73, 47]

for k, v in zip(itertools.product(W, C), Cost):
    d = {k: v}
    print(d)

This gives:

{('Anis', 'City1'): 53}
{('Anis', 'City2'): 27}
{('Anis', 'City3'): 13}
{('James', 'City1'): 80}
{('James', 'City2'): 47}
{('James', 'City3'): 67}
{('Arthur', 'City1'): 53}
{('Arthur', 'City2'): 73}
{('Arthur', 'City3'): 47}

If in fact what you want is a single dictionary rather than a sequence of 1-element dictionaries, then modify this to:

d = {k: v for k, v in zip(itertools.product(W, C), Cost)}
print(d)

which gives:

{('Anis', 'City1'): 53, ('Anis', 'City2'): 27, ('Anis', 'City3'): 13, ('James', 'City1'): 80, ('James', 'City2'): 47, ('James', 'City3'): 67, ('Arthur', 'City1'): 53, ('Arthur', 'City2'): 73, ('Arthur', 'City3'): 47}

or more simply (thanks to user Heap Overflow for the suggestion):

d = dict(zip(itertools.product(W, C), Cost))

Answered by alani on December 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP