I have 2 lists of ordereddict which i fetch from DB through a function and they look like below
I want to take every Flip value from second list and subtract that with all the Flip values of first list and insert the corresponding key value pair into first list where the difference between the values is lesser like below, this is to maintain the order of first list values(as they are already sorted)
In the above result, the difference is lesser for the flip value 13.513353868968 of first list , so i need to pick the key value pair of the flip value 23.513353868968 from second list which is
and place that between the first lesser difference and second lesser difference i.e it has to go next to items 8 in first list
Like this i want to fetch all the values from second list and repeat the same thing
i tried the below code , i am not able to make this work as expected, it prints out multiple lists, any help would be appreciated.
Code:
**First list printed output:** OrderedDict([('Items', '1'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 223.513353868968)]) OrderedDict([('Items', '2'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 243.513353868968)]) OrderedDict([('Items', '3'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 263.513353868968)]) OrderedDict([('Items', '4'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 323.513353868968)]) OrderedDict([('Items', '5'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 333.513353868968)]) OrderedDict([('Items', '6'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 343.513353868968)]) OrderedDict([('Items', '7'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 353.513353868968)]) OrderedDict([('Items', '8'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 13.513353868968)]) OrderedDict([('Items', '9'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 123.513353868968)]) OrderedDict([('Items', '10'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 162.513353868968)]) OrderedDict([('Items', '11'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 213.513353868968)])
Code:
**Second list printed output:** OrderedDict([('planneditems', '1'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 23.513353868968)]) OrderedDict([('planneditems', '4'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 113.513353868968)]) OrderedDict([('planneditems', '5'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 133.513353868968)]) OrderedDict([('planneditems', '6'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 213.513353868968)])
Code:
**Steps:** Take first flip value of second list : 23.513353868968 Subtract this with all the flip values of first list 23.513353868968 - 223.513353868968 = -200 (ignore the sign) 23.513353868968 - 243.513353868968 = 220 23.513353868968 - 263.513353868968 = 240 23.513353868968 - 323.513353868968 = 300 23.513353868968 - 333.513353868968 = 313 23.513353868968 - 343.513353868968 = 320 23.513353868968 - 13.513353868968 = 10 23.513353868968 - 123.513353868968 = 100 23.513353868968 - 162.513353868968 = 139 23.513353868968 - 213.513353868968 = 190
Code:
OrderedDict([('planneditems', '1'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 23.513353868968)])
Like this i want to fetch all the values from second list and repeat the same thing
Code:
**New List expected printed output:** OrderedDict([('Items', '1'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 223.513353868968)]) OrderedDict([('Items', '2'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 243.513353868968)]) OrderedDict([('Items', '3'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 263.513353868968)]) OrderedDict([('Items', '4'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 323.513353868968)]) OrderedDict([('Items', '5'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 333.513353868968)]) OrderedDict([('Items', '6'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 343.513353868968)]) OrderedDict([('Items', '7'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 353.513353868968)]) OrderedDict([('Items', '8'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 13.513353868968)]) OrderedDict([('planneditems', '1'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 23.513353868968)]) OrderedDict([('planneditems', '4'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 113.513353868968)]) OrderedDict([('Items', '9'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 123.513353868968)]) OrderedDict([('Items', '10'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 162.513353868968)]) OrderedDict([('Items', '11'), ('Itemspaid', 'Rutr'), ('First', 'Het'), ('Second', 'Fru'), ('Third', 'yurn'), ('Flip', 213.513353868968)]) OrderedDict([('planneditems', '6'), ('plannedItemspaid', 'pRutr'), ('PlannedFirst', 'pHet'), ('PlannedSecond', 'pFru'), ('PlannedThird', 'pyurn'), ('Flip', 213.513353868968)])
i tried the below code , i am not able to make this work as expected, it prints out multiple lists, any help would be appreciated.
Code:
for G in List2: diffs = [] indexvalue=List2.index(G) for i in range(1, len(List1)): d1 = (G['Flip'] - List1[i - 1]['Flip']) d2 = (G['Flip'] - List1[i]['Flip']) t = (d1 + d2, i) diffs.append(t) j = min(diffs)[1] new_xs=List1[0:j] +[List2[indexvalue]]+ List1[j:] print(new_xs)
Comment