Style Frage 2D Array

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
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
mawe
Python-Forum Veteran
Beiträge: 1209
Registriert: Montag 29. September 2003, 17:18
Wohnort: Purkersdorf (bei Wien [Austria])

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
Viel

thx! so siehts schon schön aus.
CM
User
Beiträge: 2464
Registriert: Sonntag 29. August 2004, 19:47
Kontaktdaten:

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
Antworten