Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
maintaining list order
#1
Simple comparison thingy. I have two lists and I want to compare if a value is larger than a value in the other list. The result is correct, however the structure is broken, I want the structure of the result list the same as the elements list.

comparelist = [0, 3000, 6000]
elements = [[3], [4, 400], [3,4222]]
result = []

for element in elements:    
    for e in element:
    	t = []
        count = -1       
        for c in comparelist:
            if e > c:
                count +=1
            else:
                t.append(comparelist[count])
                break    	
	result.append(t)
print(result)

#Results in [[0], [0], [0], [0], [3000]]

#I need [[0], [0, 0], [0, 3000]]
Somewhat more pythonic way to write things ( I guess ... still learning )

comparelist = [0, 3000, 6000]
elements = [[3], [4, 400], [3,4222]]
result = []

for element in elements:    
    for e in element:
    	t = []           
        for index, value in enumerate(comparelist,-1):
            if e > value:
                pass
            else:
                t.append(comparelist[index])
                break    	
	result.append(t)
print(result)
What am I doing wrong / what am I missing?
Reply
#2
My brain is too sluggish, I don't get which principles are applied to reach needed outcome.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
@perfringo: same.

And things actually don't work.

comparelist = [0, 3000, 6000]
elements = [[3], [4, 400], [3,7000]]
result = []
 
for element in elements:    
    for e in element:
        t = []           
        for index, value in enumerate(comparelist,-1):
            if e > value:
                pass
            else:
                t.append(comparelist[index])
                break       
    result.append(t)
print(result)

#should return [[0], [0,0], [0,6000]]

Hmmm this seems to work. Would you guys accept this (properly done / approach)?

comparelist = [0, 3000, 6000]
elements = [[3], [4000, 400], [3001,7000]]
ilist = comparelist[::-1]

result = [] 
for element in elements:  
    t = []
    for e in element:   
        for index, value in enumerate(ilist):
            if e > value:
                t.append(ilist[index]) 
                break                 
    result.append(t)
print(result)
Reply
#4
I am trying to understand (and fail) what is compared with what to get:

[3] --> [0]
[4, 400] --> [0, 0]
[3, 7000] --> [0, 6000]

I am not looking at code, because I don't understand what this code must accomplish.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
This gives exactly the result (and what I want)

https://pyfiddle.io/fiddle/a0035436-38ec...4f/?i=true

And since I love to understand coding, is this the 'most' logical approach?
Reply
#6
@3Printer, you need to explain in plain language how you produce the desired outcome. Posting a code that not work does not help to understand the logic to produce the desired outcome
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
@buran

I am no programmer, nor is it my profession, so for sure I'm explaining things in strange way, sorry. This post has already a timeline where I posted updates / thoughts, so please do read along.

I thought in my first post I explained what I
- want
I have two lists and I want to compare if a value is larger than a value in the other list.
(
and look at first post python bottom comment:
#Results in [[0], [0], [0], [0], [3000]]
#I need [[0], [0, 0], [0, 3000]]"
)

- bump into
The result is correct, however the structure is broken, I want the structure of the result list the same as the elements list.

- got
posted code

After two hours of fiddling, puzzling
(see my post #2 ... which is actually a merged post of two ... done by your forum ... probably due short time posting?)
I noticed that my first coding actually didn't work as I stated in my original post aka my writing of "the result is correct" is false.
post #2.1
Afterwards I came up with a solution myself which gives the desired output
post #2.2

So I altered my question in my last post to: "And since I love to understand coding, is this the 'most' logical approach?", posting a link with working example.

To my understanding things are pretty clear, posting not non-working-not-explained coding. Perhaps I should deleted this whole topic and start a new topic asking the same question: "is this the 'most' logical approach"
Reply
#8
Sorry, I still don't get it. I have no idea what you compare with what to achieve desired output.

Can You describe in spoken language what you want to accomplish. What comparison are made to get from first two rows third row:

[ 0,  3000,     6000]
[[3], [4, 400], [3,4222]]
[[0], [0, 0],   [0, 3000]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
I think that 1 list contains thresholds (e.g. [0, 3000, 6000]), and the goal is to substitute values (from other lists) with the highest threshold (that must be lower than the value itself).
Reply
#10
Sure no problem.

I'm looping through my list and find 3 items (also lists):

Listitem 1: [3]
check if "3" is larger than a value in list [0,3000,6000]. If it's larger: return that compared value.

3 > 6000 ? no
3 > 3000 ? no
3 > 0 ? yes, return [0]

Listitem 2: [4:400]
check if "4" is larger than a value in list [0,3000,6000]. If it's larger: return that compared value.

4 > 6000 ? no
4 > 3000 ? no
4 > 0 ? yes, return 0

check if "400" is larger than a value in list [0,3000,6000]. If it's larger: return that compared value.

400 > 6000 ? no
400 > 3000 ? no
400 > 0 ? yes, return 0

These two items combined as returned value [0, 0]

Listitem 3: [3:4222]
check if "3" is larger than a value in list [0,3000,6000]. If it's larger: return that compared value.

3 > 6000 ? no
3 > 3000 ? no
3 > 0 ? yes, return 0

check if "4222" is larger than a value in list [0,3000,6000]. If it's larger: return that compared value.

4222 > 6000 ? no
4222 > 3000 ? yes, return 3000
don't test the rest, I'm satisfied with this result.

These two items combined as returned value [0, 3000]

Combined return should be a list therefor: [ [0], [0, 0], [0, 3000] ]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copying the order of another list with identical values gohanhango 7 1,168 Nov-29-2023, 09:17 PM
Last Post: Pedroski55
  Why do I have to repeat items in list slices in order to make this work? Pythonica 7 1,354 May-22-2023, 10:39 PM
Last Post: ICanIBB
  Order a list with successive permutations based on another list yvrob 3 2,816 Mar-19-2021, 08:20 AM
Last Post: supuflounder
  Group List Elements according to the Input with the order of binary combination quest_ 19 6,503 Jan-28-2021, 03:36 AM
Last Post: bowlofred
  list approach due nested order 3Pinter 6 2,847 Oct-07-2019, 01:49 PM
Last Post: 3Pinter
  How to count and order numbers in a list rachyioli 2 2,560 Aug-21-2019, 10:51 AM
Last Post: perfringo
  Randomise network while maintaining topology of nodes using edge-swap approach Tom1988 3 4,133 May-25-2017, 10:59 PM
Last Post: ichabod801

Forum Jump:

User Panel Messages

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