简体   繁体   中英

Create a dictionary with 4 keys assigning values from a list

I have a list that is 400+ items, but I need to ultimately parse it into a dataframe in pandas with 4 columns. So I thought it would be good to convert the list so that each of the 4 items received the appropriate value from the list.

list = ['1','ABC','ABC001,'ABC002,'2','ABC','ABC001','ABC003','3','DEF','DEF001','DEF001']

I want to make a dictionary that looks like the following:

Dict = {'Id' : '1', 'TopSite' : 'ABC', 'Floor' : 'ABC001', 'Room' : 'ABC002', 'Id' : '2'.... }

So I tried the following:

columns = ['Id', 'TopSite', 'Floor', 'Room']
list = ['1','ABC','ABC001,'ABC002,'2','ABC','ABC001','ABC003','3','DEF','DEF001','DEF001']
DataDict = [dict(zip(columns, regionDataList))]

The problem is that it only gives me the very first 4 items in the list in the dictionary.

You are just missing a step to iterate through your list so you can repeat the dict(zip(...)) for each chunk of items corresponding to the number of columns (assuming that what you really want is a list of dicts - a bit different than your example output). Something like:

cols = ['Id', 'TopSite', 'Floor', 'Room']
items = ['1','ABC','ABC001','ABC002','2','ABC','ABC001','ABC003','3','DEF','DEF001','DEF001']

results = [dict(zip(cols, items[i:i + len(cols)])) for i in range(0, len(items), len(cols))]

print(results)
# OUTPUT
# [{'Id': '1', 'TopSite': 'ABC', 'Floor': 'ABC001', 'Room': 'ABC002'}, {'Id': '2', 'TopSite': 'ABC', 'Floor': 'ABC001', 'Room': 'ABC003'}, {'Id': '3', 'TopSite': 'DEF', 'Floor': 'DEF001', 'Room': 'DEF001'}]

Of course, you really don't need the interim step if your goal is to convert to dataframe. You can get there by passing your columns list as parameter along with a chunked up version of your items list. For example:

import pandas as pd

cols = ['Id', 'TopSite', 'Floor', 'Room']
items = ['1','ABC','ABC001','ABC002','2','ABC','ABC001','ABC003','3','DEF','DEF001','DEF001']
chunks = [items[i:i + len(cols)] for i in range(0, len(items), len(cols))]

df = pd.DataFrame(chunks, columns=cols)
print(df)
# OUTPUT
#   Id TopSite   Floor    Room
# 0  1     ABC  ABC001  ABC002
# 1  2     ABC  ABC001  ABC003
# 2  3     DEF  DEF001  DEF001

As an aside, both examples will run faster for large lists if you handle the len(...) bits with variables rather than running them over and over inside the comprehensions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM