mardi 28 juin 2016

python how to sort differents lists with a single unique value and append it to a result list

I have several lists with unique values

>>>print(ad)
5
>>>print(ae)
2
>>>print(af)
3
>>>print(ag)
4
>>>print(ah)
1
>>>print(ai)
6
>>>print(aj)
7

I'm trying to sort them and append them to a empty list results, if one of the list is bigger than the other one.

If ad is bigger than ae, then append ad to results

I wrote that

    for i in range(7):
        if not results:
            results.append(ad)
        else:    
            if ad > results[i]  and ad not in results:
                results.append(ad)
            elif ae > results[i]  and ae not in results:
                results.append(ae)
            elif af > results[i]  and af not in results:
                results.append(af)
            elif ag > results[i]  and ag not in results:
                results.append(ag)  
            elif ah > results[i]  and ah not in results:
                results.append(ah)
            elif ai > results[i]  and ai not in results:
                results.append(ai)
            elif aj > results[i]  and aj not in results:
                results.append(aj)

Issue is I have the following error as you may already guessed ...

IndexError: list index out of range

I hear you, how can it append when the index does not exist yet...

So I came up with that trick

for i in range(7):
    if len(results) < i:
        results.append(i)

Yet... it will not do the comparison anymore, it will just append

I've been thinking about it since Friday and I feel lost.

Any ideas that can unblock me from that ourobouros are welcome

Thanks

Aucun commentaire:

Enregistrer un commentaire