maintaining list order

Wenn du dir nicht sicher bist, in welchem der anderen Foren du die Frage stellen sollst, dann bist du hier im Forum für allgemeine Fragen sicher richtig.
Antworten
tutu10
User
Beiträge: 1
Registriert: Freitag 21. Dezember 2018, 08:36

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.

Code: Alles auswählen

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 )

Code: Alles auswählen

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?
Benutzeravatar
noisefloor
User
Beiträge: 4190
Registriert: Mittwoch 17. Oktober 2007, 21:40
Wohnort: WW
Kontaktdaten:

Hi,

@tutu10: in case you did not realize - the language of this board is German, not English. Although it's not impossible that you get an answer, it a bit more unlikely.

Gruß, noisefloor
Benutzeravatar
__blackjack__
User
Beiträge: 14044
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@tutu10: Could you explain what you actually want to do/solve here? I don't understand why/how you would arrive at ``[[0], [0, 0], [0, 3000]]`` as result for the given input data. The structure seems to be the same as ``elements`` but why/how do you want to decide if an element is 0 or something different?
„A life is like a garden. Perfect moments can be had, but not preserved, except in memory. LLAP” — Leonard Nimoy's last tweet.
Benutzeravatar
ThomasL
User
Beiträge: 1378
Registriert: Montag 14. Mai 2018, 14:44
Wohnort: Kreis Unna NRW

Just initialize the empty list "t" at the right spot :-)

Code: Alles auswählen

comparelist = [0, 3000, 6000]
elements = [[3], [4, 400], [3,4222]]
result = []
 
for element in elements:
    t = []
    for e in element:
        for index, value in enumerate(comparelist,-1):
            if e > value:
                pass
            else:
                t.append(comparelist[index])
                break
    result.append(t)
    
print(result)
Ich bin Pazifist und greife niemanden an, auch nicht mit Worten.
Für alle meine Code Beispiele gilt: "There is always a better way."
https://projecteuler.net/profile/Brotherluii.png
Antworten