python - Sort nested dictionaries and replace dictioary values with ordered numbers -
not sure if problem sound bit tricky..my requirement this: have 3 columns of data in txt file below:
col1,col2,col3/n 11,0.95,21/n 11,0.75,22/n 11,0.85,23/n 11,0.65,24/n 12,0.63,22/n 12,0.75,24/n 12,0.45,25/n ...
col1 can viewed dict keys repeat <= 5 times, col3 can viewed nested dict keys values in col2, i.e. each key in col1 has <= 5 pairs of (col2: col3).
i sort nested dictionary col2 , replace col2 values highest ranking, i.e.: don't care values in col2, care ranking of col3 each col1 value:
col1,col2,col3 11,1,21/n 11,2,23/n 11,3,22/n 11,4,24/n 12,1,24/n 12,2,22/n 12,3,25/n ...
i tried turning data nested dictionaries like:
{col1:{col3:col2}} {11:{21:0.95,22:0.75,23:0.85,24:0.65},12:{22:0.63,24:0.75,25:0.45}}
i have searched around , found solutions sort nested dict etc., cannot replace values rankings either...can please help?
well, here way in basic python:
in [90]: col1 out[90]: [11, 11, 11, 11, 12, 12, 12] in [91]: col2 out[91]: [0.95, 0.75, 0.85, 0.65, 0.63, 0.75, 0.45] in [92]: col3 out[92]: [21, 22, 23, 24, 22, 24, 25]
let's create data
consisting of items each columns:
in [163]: data = [*zip(col1, col2, col3)]
in [164]: data out[164]: [(11, 0.95, 21), (11, 0.75, 22), (11, 0.85, 23), (11, 0.65, 24), (12, 0.63, 22), (12, 0.75, 24), (12, 0.45, 25)]
let's use itertools
module group them up:
in [174]: import itertools in [175]: groups = itertools.groupby(data, key=lambda x: x[0])
now, groups
generator. if want see looks like
need iterate it:
for a, b, in groups: print(a, list(b))
and get:
11 [(11, 0.95, 21), (11, 0.75, 22), (11, 0.85, 23), (11, 0.65, 24)] 12 [(12, 0.63, 22), (12, 0.75, 24), (12, 0.45, 25)]
but exhausted iterator. let's create again, ,
know contains, can perform desired sorting:
in [177]: groups = itertools.groupby(data, key=lambda x: x[0]) in [178]: groups2 = [sorted(list(b), reverse=true) a, b in groups] in [179]: groups2 out[179]: [[(11, 0.95, 21), (11, 0.85, 23), (11, 0.75, 22), (11, 0.65, 24)], [(12, 0.75, 24), (12, 0.63, 22), (12, 0.45, 25)]]
ok, 1 more thing, , in editor:
for in range(len(groups2)): groups2[i] = [(x, i, z) i, (x, y, z) in enumerate(groups2[i], 1)] g in groups2: item in g: print(item)
and get:
(11, 1, 21) (11, 2, 23) (11, 3, 22) (11, 4, 24) (12, 1, 24) (12, 2, 22) (12, 3, 25)
Comments
Post a Comment