Python Forum
Creating new list from 2 lists after a custom sort
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating new list from 2 lists after a custom sort
#1
I have 2 lists
Quote:list1=[25,54,68,78,12]
list2=[35,11,18,70]

i want to take every no from list2 and subtract that with every no in list1 and find the difference and place that list2 number in between the numbers of list1 which has least difference to make sure that list1 maintains the sort as it is

newlist=[25,35,54,68,70,78,11,12,18]

steps are as follows
Quote:take the no 35 from list2
subtract that with every no in list1
35-25 = 10
35-54= -19
35-68 = -33
35-78=-43
35-12=23

Here we should ignore the negative symbol and take the number, in this case the difference is lowest with 25, so the number 35 should be placed between 25 and 54 in list1 so the new list after this iteration looks like
Quote:newlist=[25,35,54,68,78,12]

Like this we need to take every other number from list2 and repeat the same step to get the expected output

How can i do this
Reply
#2
any help on this?
Reply
#3
You didn't post any code. What have you tried?
Reply
#4
I have 2 lists of ordereddict

Quote:First list sample :

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)])

Second list sample:

Quote: 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)])

I want to take every Flip value from second list and subtract that with all the Flip values of first list and insert the complete set into first list where the difference between the values is lesser like below, this is to maintain the order of first list values(they are already sorted)

New List Sample:


Quote: 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)])

I tried the below code, but it is not working as expected, any help would be appreciated

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)
Reply
#5
Oh, this is too complicated.

You need zip() and a for-loop.
Read first, what zip does.

list3 = []
for list_element1, list_element2 in zip(list1, list2):
    result = list_element2 - list_element1
    list3.append(result)
To get flip from OrderedDict1 and 2, you can use also a for loop.
Should I post the solution?

Just try to iterate over the list with the OrderedDict inside and use the getitem access (square brackets) to get flip.
Then you need to convert the string to an integer or float. Then you append it to list1 and list2.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#6
(May-28-2018, 01:54 PM)DeaD_EyE Wrote: Oh, this is too complicated.

You need zip() and a for-loop.
Read first, what zip does.

list3 = []
for list_element1, list_element2 in zip(list1, list2):
    result = list_element2 - list_element1
    list3.append(result)
To get flip from OrderedDict1 and 2, you can use also a for loop.
Should I post the solution?

Just try to iterate over the list with the OrderedDict inside and use the getitem access (square brackets) to get flip.
Then you need to convert the string to an integer or float. Then you append it to list1 and list2.


can you pls post the solution :(
Reply
#7
any help on this?
Reply
#8
(May-26-2018, 09:57 PM)micseydel Wrote: You didn't post any code. What have you tried?

Posted
Reply
#9
any help on this?
Reply
#10
# list1 with OrderedDicts as its content
# list2 with OrderedDicts as its content

list1_flip = []
list2_flip = []

for element in list1:
    flip = element['Flip']
    list1_flip.append(flip)
or you use zip to make both lists with one for loop.

# list1 with OrderedDicts as its content
# list2 with OrderedDicts as its content

list1_flip = []
list2_flip = []

for element1, element2 in zip(list1, list2):
    flip1 = element1['Flip']
    flip2 = element2['Flip']
    list1_flip.append(flip1)
    list2_flip.append(flip2)
Written as function:



def get_flip(list1, list2):
    list1_flip = []
    list2_flip = []

    for element1, element2 in zip(list1, list2):
        flip1 = element1['Flip']
        flip2 = element2['Flip']
        list1_flip.append(flip1)
        list2_flip.append(flip2)
    return list1_flip, list2_flip

flip1, flip2 = get_flip(your_list1, your_list2)
And a shorter version as generator:



def get_flip(list1, list2):
    for element1, element2 in zip(list1, list2):
        flip1 = element1['Flip']
        flip2 = element2['Flip']
        yield flip1, flip2

flip1, flip2 = get_flip(your_list1, your_list2)
# because of the tuple unpacking on the left side,
# Python iterates automatically over the generator get_flip
Code not tested, just written it down.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  list.sort() returning None SmallCoder14 8 600 Mar-19-2024, 09:49 PM
Last Post: SmallCoder14
  Sort a list of dictionaries by the only dictionary key Calab 1 498 Oct-27-2023, 03:03 PM
Last Post: buran
  List all possibilities of a nested-list by flattened lists sparkt 1 923 Feb-23-2023, 02:21 PM
Last Post: sparkt
  user input values into list of lists tauros73 3 1,075 Dec-29-2022, 05:54 PM
Last Post: deanhystad
  returning a List of Lists nafshar 3 1,076 Oct-28-2022, 06:28 PM
Last Post: deanhystad
  Creating list of lists, with objects from lists sgrinderud 7 1,657 Oct-01-2022, 07:15 PM
Last Post: Skaperen
Photo a.sort() == b.sort() all the time 3lnyn0 1 1,328 Apr-19-2022, 06:50 PM
Last Post: Gribouillis
  list sort() function bring backs None CompleteNewb 6 4,150 Mar-26-2022, 03:34 AM
Last Post: Larz60+
  How to efficiently average same entries of lists in a list xquad 5 2,139 Dec-17-2021, 04:44 PM
Last Post: xquad
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,927 Oct-03-2021, 05:16 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020