1

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....}
2
  • 1
    Beside the point, but you're missing a closing bracket to match GR = [ Commented Jan 21, 2019 at 21:21
  • 2
    Please add the desired output. Commented Jan 21, 2019 at 21:22

5 Answers 5

1

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]}]
Sign up to request clarification or add additional context in comments.

3 Comments

like this with ```, called the "backtick" it is shift+tilde on most western boards
This is how I was able to return what was being asked, took me a little while to debug but should work: GL = dict() for item in GR[1:]: GL[item[0]] = [int(item[1]),int(item[2]),int(item[3])] print(GL)
@Mac if you answered your own question (or if you found an answer among the ones here) you should mark it as answered. (You can post an answer to your own question and mark it as the answer, that's allowed)
1

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:]}

Comments

0

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]}

Comments

0

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]}

Comments

0

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)

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.