Home > Back-end >  Construct dictionary from some elements in list in Python
Construct dictionary from some elements in list in Python

Time:09-14

I have a list like this:

['db_name1', ['132K', 'user1'], ['8.0K', 'user1'], ['16K', 'user2'], 'db_name2', ['132K', 'user3'], ['292K', 'user3'], ['16K', 'user4'], ['132K', 'user4'], 'db_name3', ['132K', 'user5'], ['12K', 'user5'], ['16K', 'user6']]

For easier understanding, I'll print each entry as a row:

db_name1
['132K', 'user1']
['4.0M', 'user1']
['40K', 'user2']
.
.
db_name2
['132K', 'user3']
['3.6M', 'user4']
['48K', 'user4']
.
.

db_name3
['132K', 'user5']
['16M', 'user5']
.
.

Each row is an element from the list and it has some rows as strings (db_name) and the next rows (until next db_name) are lists containing two elements (size and username).

Is there a way to construct a dictionary like this?

{"db_name1" : { "user1" : ["132K", "4.0M" .....]
               "user2" : ["40K", "4.0M" .....]
             }
 "db_name2" : { "user3" : ["132K", "3.6M" .....]
               "user4" : ["48K", "4.0M" .....]
             }
}

How should I take respective db_names from the list and use them as keys for the dictionary, then add as value, for each one another dictionary containing username as key and respective sizes as value?

I have a list of indexes with positions for db_names entires inside the main list.

dbs_indexes = [0, 5, 23, 1360, 1364, 4124, 4680, 5294, 13212, 13219]

Should I use this to help with this?

CodePudding user response:

Can you try the following:

final_results = {}
db_name1 = [
    ['132K', 'user1'],
    ['4.0M', 'user1'],
    ['40K', 'user2']
]
df1 = pd.DataFrame(db_name1, columns=['values', 'username'])
final_results['db_name1'] = df1.groupby('username')['values'].apply(list).to_dict()

You can do the same for other db_name's

Edit:

Entire code based on your example:

import pandas as pd

all_data = ['db_name1', ['132K', 'user1'], ['8.0K', 'user1'], ['16K', 'user2'], 'db_name2', ['132K', 'user3'], ['292K', 'user3'], ['16K', 'user4'], ['132K', 'user4'], 'db_name3', ['132K', 'user5'], ['12K', 'user5'], ['16K', 'user6']]

fil_data = {}
for item in all_data:
  if isinstance(item, str):
    key = item
    fil_data[key] = []
  if isinstance(item, list):
    fil_data[key].append(item)

final_results = {}
for key, value in fil_data.items():
  df1 = pd.DataFrame(value, columns=['values', 'username'])
  final_results[key] = df1.groupby('username')['values'].apply(list).to_dict()

print(final_results)

Ouptut:

{'db_name1': {'user1': ['132K', '8.0K'], 'user2': ['16K']},
 'db_name2': {'user3': ['132K', '292K'], 'user4': ['16K', '132K']},
 'db_name3': {'user5': ['132K', '12K'], 'user6': ['16K']}}
  • Related