Seite 1 von 1

random.shuffle bei tuppeln

Verfasst: Freitag 15. Mai 2009, 15:38
von alpha
Hallo Leute,

wenn ich sowas:

Code: Alles auswählen

d = (   
            [1,["hallo1",0],["Hello1",0],["/picpath/pic1.jpg",0]],
            [2,["hallo2",0],["Hello2",0],["/picpath/pic2.jpg",0]],
            [3,["hallo3",0],["Hello3",0],["/picpath/pic3.jpg",0]],
            [4,["hallo4",0],["Hello4",0],["/picpath/pic4.jpg",0]],
            [5,["hallo5",0],["Hello5",0],["/picpath/pic5.jpg",0]]
        ) 
nach der vordersten Zahl geshuffelt haben will (also zufällig sortiert), wie bekomme ich das hin?

Code: Alles auswählen

import random
random.shuffle(d)
führt immer zu einem Fehler.. ich hab keine Ideen mehr. Weiß jemand Rat?

Danke und Grüße
alpha

Verfasst: Freitag 15. Mai 2009, 15:44
von Nocta
Ich weiß Rat, les die Fehlermeldung:

Code: Alles auswählen

TypeError: 'tuple' object does not support item assignment
und les die Dokumentation ;)

Mit Tupeln geht das ganze jedenfalls nicht, benutze eine Liste und gut ist.

Eine Frage zur shuffle - Methode

Verfasst: Freitag 15. Mai 2009, 19:31
von Pascal
Hallo,

ich hab auch eine Frage zu random.shuffle

und zwar: wie geht das eigentlich?

mein Problem ist folgendes:

Code: Alles auswählen

>>> import random
>>> k=range(11)
>>> print random.shuffle(k)
None
>>> # das sagt die helpfunktion auch vorraus:
>>> help(random.shuffle)
Help on method shuffle in module random:

shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
    x, random=random.random -> shuffle list x in place; return None.


Aber wie kann ich die liste jetzt shuffeln??

Re: Eine Frage zur shuffle - Methode

Verfasst: Freitag 15. Mai 2009, 19:34
von numerix
Pascal hat geschrieben:Hallo,

ich hab auch eine Frage zu random.shuffle

und zwar: wie geht das eigentlich?

mein Problem ist folgendes:

Code: Alles auswählen

>>> import random
>>> k=range(11)
>>> print random.shuffle(k)
None
>>> # das sagt die helpfunktion auch vorraus:
>>> help(random.shuffle)
Help on method shuffle in module random:

shuffle(self, x, random=None, int=<type 'int'>) method of random.Random instance
    x, random=random.random -> shuffle list x in place; return None.
Aber wie kann ich die liste jetzt shuffeln??
Na, die Lösung steht doch da: Die Liste wird "in place" durcheinandergewürfelt. Den Rückgabewert kannst du nicht gebrauchen.

Edit:
Lies das doch mal (ist auch ein Beispiel für shuffle dabei): http://docs.python.org/library/random.html

Verfasst: Freitag 15. Mai 2009, 19:45
von Pascal
ja danke.

ja... klingt ja auch logisch ^^

Verfasst: Freitag 15. Mai 2009, 23:13
von alpha
Ah.. alles klar.. was so ein ersetzen von ( in [ ausmacht :-)

Danke für den Tip

alpha