Iterate over two dictionaries in one loop in python -
i have 2 dictionaries. 1 has chapter_id , book_id: {99: 7358, 852: 7358, 456: 7358}
. here 1 book example, there many. , 1 same chapter_id , information: {99: [john smith, 20, 5], 852: [clair white, 15, 10], 456: [daniel dylan, 25, 10]}
. chapter ids unique through books. , have combine in way every book gets information chapters contains. {7358:[[99,852,456],[john smith, claire white, daniel dylan],[20,15,25],[5,10,10]]}
. have file dictionary, each book has ids of chapters has. know how looping on both dictionaries (they used lists). takes ages. why dictionaries , think can manage 1 loop on chapters. in head come looping on books , on chapters. ideas appreciated! final result write in file, not important if nested dictionary or else. or @ least think so.
if open using other packages might want have on pandas
, allow many things , fast. here example based on data provided...
import pandas pd d1 = {99: 7358, 852: 7358, 456: 7358} df1 = pd.dataframe.from_dict(d1, "index") df1.reset_index(inplace=true) d2 = {99: ["john smith", 20, 5], 852: ["clair white", 15, 10], 456: ["daniel dylan", 25, 10]} df2 = pd.dataframe.from_dict(d2, "index") df2.reset_index(inplace=true) df = df1.merge(df2, left_on="index", right_on="index") df.columns = ["a", "b", "c", "d", "e"] # data 7358 (ie subsetting) df[df.b == 7358] # names list list(df[df.b == 7358].c)
Comments
Post a Comment