Seite 1 von 1

Style Frage 2D Array

Verfasst: Dienstag 5. April 2005, 20:56
von Viel
Hi!

Ich kenne Python erst kurz, bin also noch neu.

Wie würde ihr den Code unten schreiben?
Der Code erzeugt ein 2D Array gefüllt mit Zufallszahlen.

Code: Alles auswählen

def Erzeuge_Spielfeld():
    
    import random
    
    y = 10
    x = 10
    iy = 0
    ix = 0
    feld = []
    
    while iy < y:
        spalte = []
        while ix < x:
            spalte.append(random.randint(1,5))
            ix = ix + 1
        feld.append(spalte)
        iy = iy + 1
        ix = 0
        del spalte
    return feld
        
print 'modul test.py wird ausgefuehrt'
Erzeuge_Spielfeld()
cu

Verfasst: Dienstag 5. April 2005, 21:09
von mawe
Hi!

Ich würds so in etwa machen:

Code: Alles auswählen

import random

x = 10
y = 10
feld = []
for i in range(x):
    feld.append([random.randint(1,5) for i in range(y)])
print feld
Gruß, mawe

Verfasst: Dienstag 5. April 2005, 21:32
von Viel
thx! so siehts schon schön aus.

Verfasst: Mittwoch 6. April 2005, 06:46
von CM
Hi

Wenn Du mehr "array-Power" haben willst, geht es auch mit numarray:

Code: Alles auswählen

from numarray.random_array import *
seed()
random((x,y))
Gruß,
Christian