Python Forum
Creating new list from 2 lists after a custom sort - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Creating new list from 2 lists after a custom sort (/thread-10591.html)

Pages: 1 2


Creating new list from 2 lists after a custom sort - pythoneer - May-26-2018

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


RE: Creating new list from 2 lists after a custom sort - pythoneer - May-26-2018

any help on this?


RE: Creating new list from 2 lists after a custom sort - micseydel - May-26-2018

You didn't post any code. What have you tried?


Create a list from 2 lists after custom sort - pythoneer - May-28-2018

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)



RE: Creating new list from 2 lists after a custom sort - DeaD_EyE - May-28-2018

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.


RE: Creating new list from 2 lists after a custom sort - pythoneer - May-28-2018

(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 :(


RE: Creating new list from 2 lists after a custom sort - pythoneer - May-29-2018

any help on this?


RE: Creating new list from 2 lists after a custom sort - pythoneer - May-29-2018

(May-26-2018, 09:57 PM)micseydel Wrote: You didn't post any code. What have you tried?

Posted


RE: Creating new list from 2 lists after a custom sort - pythoneer - May-29-2018

any help on this?


RE: Creating new list from 2 lists after a custom sort - DeaD_EyE - May-29-2018

# 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.