Problem mit update bei dict
Hallo, Ich möchte in einen Dictionary zwei Werte updaten. die normale Ausgabe ist so: Code: 0, 0 0, 1 0, 2 0, 3 0, 4 1, 1 . . . Wenn ich es ausprinten lasse ist das so, aber ich habe in meinen Dict sind sie nicht so, die Ausgabe ist so: Code: {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum2.png', 'x': 4} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum3.png', 'x': 0} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum2.png', 'x': 4} Warum? Ich sehe da keine 1, 1 und so. Hier ist der Code: Code: #!/usr/bin/python # -*- coding: utf-8 -*- """ Generate the playingfield """ #~Imports import cPickle as pickle import random #~Define arena height_rows=5 width_cols=5 #~Define images grass = {"tile" : "/gametiles/grass.png", "action" : None, "x" : None, "y": None} baum1 = {"tile" : "/gametiles/baum1.png", "action" : None, "x" : None, "y": None} baum2 = {"tile" : "/gametiles/baum2.png", "action" : None, "x" : None, "y": None} baum3 = {"tile" : "/gametiles/baum3.png", "action" : None, "x" : None, "y": None} #~Variables row=[] for y in range(height_rows): col=[] for x in range(width_cols): number = random.randint(1, 10) if number == 5: baum1.update({"x" : x, "y" : y}) col.append(baum1) elif number == 1: baum2.update({"x" : x, "y" : y}) col.append(baum2) elif number == 7: baum3.update({"x" : x, "y" : y}) col.append(baum3) else: grass.update({"x" : x, "y" : y}) col.append(grass) row.append(col) for i in row: for i in i: print i name = raw_input("mapname: ") pickle.dump(row, open("../maps/" + name + ".sav", "wb" ) ) print "fertig"
Zwecks Vollständigkeit der Link zum entsprechenden Forums-Beitrag: http://www.python-forum.de/viewtopic.php?f=1&t=30556 Hat ja keinen Sinn, die drei Seiten hier nochmal durchzuackern... Am 3. Dezember 2012 18:30 schrieb Jonel <jtschoche@googlemail.com>:
Hallo, Ich möchte in einen Dictionary zwei Werte updaten. die normale Ausgabe ist so:
Code:
0, 0 0, 1 0, 2 0, 3 0, 4 1, 1 . . .
Wenn ich es ausprinten lasse ist das so, aber ich habe in meinen Dict sind sie nicht so, die Ausgabe ist so: Code: {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum2.png', 'x': 4} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum3.png', 'x': 0} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum2.png', 'x': 4}
Warum? Ich sehe da keine 1, 1 und so.
Hier ist der Code:
Code: #!/usr/bin/python # -*- coding: utf-8 -*-
""" Generate the playingfield """ #~Imports
import cPickle as pickle import random
#~Define arena
height_rows=5 width_cols=5
#~Define images
grass = {"tile" : "/gametiles/grass.png", "action" : None, "x" : None, "y": None} baum1 = {"tile" : "/gametiles/baum1.png", "action" : None, "x" : None, "y": None} baum2 = {"tile" : "/gametiles/baum2.png", "action" : None, "x" : None, "y": None} baum3 = {"tile" : "/gametiles/baum3.png", "action" : None, "x" : None, "y": None}
#~Variables
row=[] for y in range(height_rows): col=[] for x in range(width_cols): number = random.randint(1, 10) if number == 5: baum1.update({"x" : x, "y" : y}) col.append(baum1) elif number == 1: baum2.update({"x" : x, "y" : y}) col.append(baum2) elif number == 7: baum3.update({"x" : x, "y" : y}) col.append(baum3) else: grass.update({"x" : x, "y" : y}) col.append(grass) row.append(col) for i in row: for i in i: print i name = raw_input("mapname: ") pickle.dump(row, open("../maps/" + name + ".sav", "wb" ) ) print "fertig" _______________________________________________ python-de maillist - python-de@python.org http://mail.python.org/mailman/listinfo/python-de
Am 03.12.12 18:30, schrieb Jonel:
Hallo, Ich möchte in einen Dictionary zwei Werte updaten. die normale Ausgabe ist so:
Code:
0, 0 0, 1 0, 2 0, 3 0, 4 1, 1 . . .
Wenn ich es ausprinten lasse ist das so, aber ich habe in meinen Dict sind sie nicht so, die Ausgabe ist so: Code: {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum2.png', 'x': 4} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum3.png', 'x': 0} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum1.png', 'x': 2} {'action': None, 'y': 4, 'tile': '/gametiles/grass.png', 'x': 3} {'action': None, 'y': 4, 'tile': '/gametiles/baum2.png', 'x': 4}
Warum? Ich sehe da keine 1, 1 und so.
Hier ist der Code:
Code: #!/usr/bin/python # -*- coding: utf-8 -*-
""" Generate the playingfield """ #~Imports
import cPickle as pickle import random
#~Define arena
height_rows=5 width_cols=5
#~Define images
grass = {"tile" : "/gametiles/grass.png", "action" : None, "x" : None, "y": None} baum1 = {"tile" : "/gametiles/baum1.png", "action" : None, "x" : None, "y": None} baum2 = {"tile" : "/gametiles/baum2.png", "action" : None, "x" : None, "y": None} baum3 = {"tile" : "/gametiles/baum3.png", "action" : None, "x" : None, "y": None}
#~Variables
row=[] for y in range(height_rows): col=[] for x in range(width_cols): number = random.randint(1, 10) if number == 5: baum1.update({"x" : x, "y" : y}) col.append(baum1) elif number == 1: baum2.update({"x" : x, "y" : y}) col.append(baum2) elif number == 7: baum3.update({"x" : x, "y" : y}) col.append(baum3) else: grass.update({"x" : x, "y" : y}) col.append(grass) row.append(col) for i in row: for i in i: print i name = raw_input("mapname: ") pickle.dump(row, open("../maps/" + name + ".sav", "wb" ) ) print "fertig" _______________________________________________ python-de maillist - python-de@python.org http://mail.python.org/mailman/listinfo/python-de
Aus meiner Sicht macht es nicht viel Sinn das gleiche Dictionary immer wieder abzudaten. Das jeweils letzte Update entscheidet dann folgerichtig was in z.B. in baum1 steht. Alle anderen Updates sind sinnlos, da die Information verloren ist. Wenn dort 25 (5 x 5) verschiedene x,y-Kombinationen in den Dicts stehen sollen müssen es auch 25 Dicts sein und nicht nur 4. Wie wäre es damit?: #!/usr/bin/python # -*- coding: utf-8 -*- """ Generate the playingfield """ #~Imports import cPickle as pickle import random #~Define arena height_rows=5 width_cols=5 #~Variables object_map = {5: 'baum1', 1: 'baum2', 7: 'baum3'} rows = [] for y in range(height_rows): col = [] for x in range(width_cols): number = random.randint(1, 10) name = object_map.get(number, 'grass') col.append({"tile" : "/gametiles/%s.png" % name, "action" : None, "x" : x, "y": y}) rows.append(col) for row in rows: for col in row: print col Die Ausgabe: {'tile': '/gametiles/grass.png', 'action': None, 'y': 0, 'x': 0} {'tile': '/gametiles/baum1.png', 'action': None, 'y': 0, 'x': 1} {'tile': '/gametiles/baum1.png', 'action': None, 'y': 0, 'x': 2} {'tile': '/gametiles/grass.png', 'action': None, 'y': 0, 'x': 3} {'tile': '/gametiles/grass.png', 'action': None, 'y': 0, 'x': 4} {'tile': '/gametiles/grass.png', 'action': None, 'y': 1, 'x': 0} {'tile': '/gametiles/grass.png', 'action': None, 'y': 1, 'x': 1} {'tile': '/gametiles/grass.png', 'action': None, 'y': 1, 'x': 2} {'tile': '/gametiles/grass.png', 'action': None, 'y': 1, 'x': 3} {'tile': '/gametiles/grass.png', 'action': None, 'y': 1, 'x': 4} {'tile': '/gametiles/grass.png', 'action': None, 'y': 2, 'x': 0} {'tile': '/gametiles/grass.png', 'action': None, 'y': 2, 'x': 1} {'tile': '/gametiles/baum3.png', 'action': None, 'y': 2, 'x': 2} {'tile': '/gametiles/grass.png', 'action': None, 'y': 2, 'x': 3} {'tile': '/gametiles/grass.png', 'action': None, 'y': 2, 'x': 4} {'tile': '/gametiles/grass.png', 'action': None, 'y': 3, 'x': 0} {'tile': '/gametiles/baum3.png', 'action': None, 'y': 3, 'x': 1} {'tile': '/gametiles/grass.png', 'action': None, 'y': 3, 'x': 2} {'tile': '/gametiles/grass.png', 'action': None, 'y': 3, 'x': 3} {'tile': '/gametiles/grass.png', 'action': None, 'y': 3, 'x': 4} {'tile': '/gametiles/baum1.png', 'action': None, 'y': 4, 'x': 0} {'tile': '/gametiles/grass.png', 'action': None, 'y': 4, 'x': 1} {'tile': '/gametiles/grass.png', 'action': None, 'y': 4, 'x': 2} {'tile': '/gametiles/grass.png', 'action': None, 'y': 4, 'x': 3} {'tile': '/gametiles/baum2.png', 'action': None, 'y': 4, 'x': 4} Wolltest du so eine Ausgabe? Ist mir aus deiner Frage nicht ganz klar geworden. Viele Grüße Mike
On Dec 3, 2012, at 11:06 PM, Jonel wrote:
Am Montag, 3. Dezember 2012 22:21:04 UTC+1 schrieb Jonel:
Ja genau so vielen Dank :)
Also gar kein update in dem Fall verwenden! Ok ich glaube ich habe dies verstanden.
Das bezweifele ich. Aber die Lösung von Mike einfach mal in's Forum kopieren mit den Worten "ich habe jetzt eine Lösung" - wie das geht hast du verstanden. Aber die Hausaufgabe ist gerettet - das ist die Hautsache! Diez
participants (4)
-
Diez B. Roggisch
-
Jonel
-
Mike Müller
-
Sebastian Wiesner