2

I want to write a code to create a dictionary from a table that maps fruits to dictionaries of their quantity in different baskets.

table = [['Fruit', 'Basket1', 'Basket2', 'Basket3'],
        ['Apples', 4, 5, 6],
        ['Bananas', 7, 8, 10],
        ['Oranges', 8, 9, 2]]

Expected dictionary: {'Apples': {'Basket1':[4],'Basket2':[5],'Basket3':[6]}, 'Bananas':{'Basket1':[7],'Basket2':[8],'Basket3':[10]}, 'Oranges':{'Basket1':[8],'Basket2':[9],'Basket3':[2]}}

Expected Output:  [Apples][Basket1]= 4

Below is what I have so far. I know the last 2 lines don't make sense, but I have a hard time figuring out how to close this one out.

basket = [y for y in table[0] if y!='Fruit']
Quantity=[y[1:4] for y in table if y[0]!='Fruit']
Fruit = [y[0] for y in table if y[0]!='Fruit']
a = dict(zip(Fruit, Quantity))
b = dict(zip(a,Basket)
b
4
  • Your 'expected' dictionary is not a dictionary. It appears to be a list of dictionaries. Also, your table is what? Not valid python for sure. Commented Aug 25, 2017 at 4:42
  • Is it so hard to paste with quotes... Commented Aug 25, 2017 at 4:43
  • My apologies. My table is a list of lists, but just want to convert it to a dictionary Commented Aug 25, 2017 at 4:55
  • please make sure you want a list as an output or not. Expected Output: [Apples][Basket1]= 4 while dictionary is of lists. Commented Aug 25, 2017 at 5:07

3 Answers 3

1

Assuming you don't care about the table header row, you can use a dictionary comprehension and enumerate the table rows:

>>> {row[0]: {'Basket{}'.format(n): [v] for n, v in enumerate(row[1:], start=1)}
     for row in table[1:]}
{'Apples': {'Basket1': [4], 'Basket2': [5], 'Basket3': [6]},
 'Bananas': {'Basket1': [7], 'Basket2': [8], 'Basket3': [10]},
 'Oranges': {'Basket1': [8], 'Basket2': [9], 'Basket3': [2]}}

If you must match the headers, then this approach should work:

headers = table[0][1:]
{row[0]: {header: [v] for header, v in zip(headers, row[1:])}
          for row in table[1:]}
Sign up to request clarification or add additional context in comments.

Comments

0
table = [['Fruit', 'Basket1', 'Basket2', 'Basket3'],
        ['Apples', 4, 5, 6],
        ['Bananas', 7, 8, 10],
        ['Oranges', 8, 9, 2]]
m={}                                      
for i in range(1,len(table)):
    if table[i][0] not in m:
        m[table[i][0]]={}
    for j in range(1,len(table[i])):
        #print m[table[i][0]]
        if table[i][j] not in m[table[i][0]]:
            m[table[i][0]][table[0][j]]=table[i][j]
print m

Comments

0

You can split the header from the rows of data and zip() the header with each row, e.g.:

In []:
header, *rows = table
{k: dict(v) for row in rows for (_, k), *v in [list(zip(header, row))]}

Out[]:
{'Apples': {'Basket1': 4, 'Basket2': 5, 'Basket3': 6},
 'Bananas': {'Basket1': 7, 'Basket2': 8, 'Basket3': 10},
 'Oranges': {'Basket1': 8, 'Basket2': 9, 'Basket3': 2}}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.