Mac
Mac

Reputation: 23

Python - Creating dictionary from numpy table to display lists

Given the following numpy table

GR = [
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['PersonA', '100', '90', '80'],
    ['PersonB', '88', '99', '111'],
    ['PersonC', '45', '56', '67'],
    ['PersonD', '59', '61', '67'],
    ['PersonE', '73', '79', '83'],
    ['PersonF', '89', '97', '101']
     ]

I need to create a dictionary named GL that maps names of students to lists of their exam grades. grades should be converted from str to int.

Desired Output: {'PersonA':[100, 90, 80], 'PersonB':[88, 99, 111], ect....}

Upvotes: 1

Views: 109

Answers (5)

Mac
Mac

Reputation: 23

My apologies @Charles Landau, my python skill has improved drastically since asking this question.

GL = dict() 
     for i in GR[1:]:
          GL[i[0]] = [int(i[1]),int(i[2]),int(i[3])]
     print(GL)

Upvotes: 0

hpaulj
hpaulj

Reputation: 231335

Your GR is a numpy array (and the use of a[2,0] indexing confirms that):

In [179]: GR
Out[179]: 
array([['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
       ['PersonA', '100', '90', '80'],
       ['PersonB', '88', '99', '111'],
       ['PersonC', '45', '56', '67'],
       ['PersonD', '59', '61', '67'],
       ['PersonE', '73', '79', '83'],
       ['PersonF', '89', '97', '101']], dtype='<U7')

So what you are doing is:

In [185]: {row[0]: row[1:] for row in GR[1:]}
Out[185]: 
{'PersonA': array(['100', '90', '80'], dtype='<U7'),
 'PersonB': array(['88', '99', '111'], dtype='<U7'),
 'PersonC': array(['45', '56', '67'], dtype='<U7'),
 'PersonD': array(['59', '61', '67'], dtype='<U7'),
 'PersonE': array(['73', '79', '83'], dtype='<U7'),
 'PersonF': array(['89', '97', '101'], dtype='<U7')}

The arrays of strings can easily be converted to integers with:

In [186]: {row[0]: row[1:].astype(int) for row in GR[1:]}
Out[186]: 
{'PersonA': array([100,  90,  80]),
 'PersonB': array([ 88,  99, 111]),
 'PersonC': array([45, 56, 67]),
 'PersonD': array([59, 61, 67]),
 'PersonE': array([73, 79, 83]),
 'PersonF': array([ 89,  97, 101])}

and on to lists:

In [187]: {row[0]: row[1:].astype(int).tolist() for row in GR[1:]}
Out[187]: 
{'PersonA': [100, 90, 80],
 'PersonB': [88, 99, 111],
 'PersonC': [45, 56, 67],
 'PersonD': [59, 61, 67],
 'PersonE': [73, 79, 83],
 'PersonF': [89, 97, 101]}

My nomination for the list conversion is:

In [188]: {name: [int(i) for i in grades] for name, *grades in GR[1:]}
Out[188]: 
{'PersonA': [100, 90, 80],
 'PersonB': [88, 99, 111],
 'PersonC': [45, 56, 67],
 'PersonD': [59, 61, 67],
 'PersonE': [73, 79, 83],
 'PersonF': [89, 97, 101]}

Upvotes: 0

Mykola Zotko
Mykola Zotko

Reputation: 17794

Option 1:

GR_ = {i[0]: list(map(int, i[1:])) for i in GR[1:]}

Option 2:

GR_ = dict(map(lambda x: (x[0], list(map(int, x[1:]))), GR[1:]))

Output:

{'PersonA': [100, 90, 80],
 'PersonB': [88, 99, 111],
 'PersonC': [45, 56, 67],
 'PersonD': [59, 61, 67],
 'PersonE': [73, 79, 83],
 'PersonF': [89, 97, 101]}

Upvotes: 0

aydow
aydow

Reputation: 3801

An alternative solution with Python 3 using map to convert the grades from strs to ints and partial unpacking to split each list into a person and their grades

In [111]: {person: list(map(int, grades)) for person, *grades in GR[1:]}
Out[111]:
{'PersonA': [100, 90, 80],
 'PersonB': [88, 99, 111],
 'PersonC': [45, 56, 67],
 'PersonD': [59, 61, 67],
 'PersonE': [73, 79, 83],
 'PersonF': [89, 97, 101]}

Python 2 equivalent

{g[0]: map(int, g[1:]) for g in GR[1:]}

Upvotes: 1

Charles Landau
Charles Landau

Reputation: 4265

I'm having a little trouble understanding but I think this is what you're looking for:

GL = [
    {x[0]: [int(j) for j in x[1:]]} for x in GR[1:]
]

Output:

[{'PersonA': [100, 90, 80]},
 {'PersonB': [88, 99, 111]},
 {'PersonC': [45, 56, 67]},
 {'PersonD': [59, 61, 67]},
 {'PersonE': [73, 79, 83]},
 {'PersonF': [89, 97, 101]}]

Upvotes: 1

Related Questions