Home > Software design >  How to transform this 2D list to a dictionary inside a dictionary?
How to transform this 2D list to a dictionary inside a dictionary?

Time:10-30

I have been trying to transform the 2D list full_info_results but doesn't seem to find a good way to do it.

full_info_results = [
    ['*', 'G02', 'G05', 'G07', 'G08', 'G10'],
    ['P001', '1', '0', '-1', '503', '1'],
    ['P067', '1', '1', '0', '-1', '503'],
    ['P218', '0', '1', '1', '-1', '-1'],
    ['P101', '0', '0', '1', '1', '503'],
    ['P456', '1', '1', '-1', '1', '-1']
]
  • GXX = game_id
  • PXXX = player_id
  • 1 = win
  • 0, -1, 503 = these 3 number doesn't mean anything.

For each player_id I want to record whether they win each particular game_id or not. For example, Player id P001 win G02 and G10. If they win, I want to record it in dictionary as 1, if not win = 0.

Expected results:

expected_result = {'P001':{'G02':1, 'G05':0, 'G07':0, 'G08':0, 'G10':1},
                   'P067':{'G02':1, 'G05':1, 'G07':0, 'G08':0, 'G10':0},
                   'P218':{'G02':0, 'G05':1, 'G07':1, 'G08':0, 'G10':0},
                   'P101':{'G02':0, 'G05':0, 'G07':1, 'G08':1, 'G10':0},
                   'P456':{'G02':1, 'G05':1, 'G07':0, 'G08':1, 'G10':0}}

I wonder what is the proper way to transform this, because I hardly think of any right now.. Thanks in advance.

CodePudding user response:

You could use the combination of dict and zip to build your sub dictionaries:

import pprint

expected_result = {}
# remove first item from full_info_results, to build the sub dict's keys 
keys = full_info_results.pop(0)[1:]
for result in full_info_results:
    # first item is the key, so remove it from the list
    key = result.pop(0)
    # convert '1' to 1 everything else to 0
    expected_result[key] = dict(
        zip(keys, [1 if r == '1' else 0 for r in result])
    )

pprint.pprint(expected_result)

Out:

{'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
 'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
 'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
 'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
 'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}

CodePudding user response:

keys= full_info_results[0][1:]
d = { l[0]: {k:(1 if v == '1' else 0) for k,v in zip(keys, l[1:])} for l in full_info_results[1:]}
In [18]: d
Out[18]:
{'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
 'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
 'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
 'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
 'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}

CodePudding user response:

Try this :

full_info_results = [
    ['*', 'G02', 'G05', 'G07', 'G08', 'G10'],
    ['P001', '1', '0', '-1', '503', '1'],
    ['P067', '1', '1', '0', '-1', '503'],
    ['P218', '0', '1', '1', '-1', '-1'],
    ['P101', '0', '0', '1', '1', '503'],
    ['P456', '1', '1', '-1', '1', '-1']
]

keys = [key for key in full_info_results[0] if key.startswith("G")]

expected_result = {}

for pi in full_info_results[1:]:
    expected_result[pi[0]] = dict(zip(keys, [1 if win == '1' else 0 for win in pi[1:]]))

print(expected_result)

Output:

{'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1}, 
'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0}, 
'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0}, 
'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0}, 
'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}

CodePudding user response:

The following is rather concise using starred assignment excessively:

(_, *keys), *data = full_info_results
{head: {k: int(v=="1") for k, v in zip(keys, tail)} for head, *tail in data}
# {'P001': {'G02': 1, 'G05': 0, 'G07': 0, 'G08': 0, 'G10': 1},
#  'P067': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 0, 'G10': 0},
#  'P101': {'G02': 0, 'G05': 0, 'G07': 1, 'G08': 1, 'G10': 0},
#  'P218': {'G02': 0, 'G05': 1, 'G07': 1, 'G08': 0, 'G10': 0},
#  'P456': {'G02': 1, 'G05': 1, 'G07': 0, 'G08': 1, 'G10': 0}}
  • Related