Python Forum

Full Version: maintaining list order
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
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?
My brain is too sluggish, I don't get which principles are applied to reach needed outcome.
@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)
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.
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?
@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
@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"
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 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).
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] ]
Pages: 1 2