基于自定义排序规则从两个OrderedDict列表生成新列表
Merge OrderedDict Lists by Closest Flip Value
Alright, let's walk through how to solve this problem where we need to insert elements from a second list of OrderedDicts into the first list, placing each element right after the entry in the first list with the closest Flip value (using absolute difference).
Approach
Here's the step-by-step logic we'll follow:
- Iterate over each element in the second list one by one.
- For each element, extract its
Flipvalue. - Calculate the absolute difference between this
Flipvalue and everyFlipvalue in the current state of the first list (since we'll be modifying the first list as we go, each insertion affects subsequent steps). - Find the index of the element in the first list with the smallest difference.
- Insert the second list element immediately after that index.
- Repeat until all elements from the second list are inserted.
Code Implementation
Let's use your sample data to demonstrate this:
from collections import OrderedDict # Your original first list list1 = [ 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)]) ] # Your original second list list2 = [ 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)]) ] # Process each element from list2 into list1 for item in list2: target_flip = item['Flip'] # Calculate absolute differences with all current entries in list1 diffs = [abs(entry['Flip'] - target_flip) for entry in list1] # Find the index of the closest entry closest_idx = diffs.index(min(diffs)) # Insert the item right after the closest entry list1.insert(closest_idx + 1, item) # Optional: Print the merged list to verify the results for pos, entry in enumerate(list1): item_id = entry.get('Items') or entry.get('planneditems') print(f"Position {pos}: Flip = {entry['Flip']}, ID = {item_id}")
Key Notes
- This approach preserves the original order of elements in
list1—we're only inserting new elements fromlist2without reordering the existing ones. - Elements from
list2are inserted in their original order, so the first element inlist2gets placed before the second, and so on. - If multiple entries in
list1have the same minimal difference to alist2element, the code will insert after the first occurrence. If you need to handle ties by inserting after the last occurrence instead, you can modify theclosest_idxcalculation to find the last index of the minimum difference value.
内容的提问来源于stack exchange,提问作者Python_newbie
相关产品推荐
相关产品推荐

