Seite 1 von 1

dynamisches erstellen von dictionaries - Python Fehler?

Verfasst: Freitag 27. Februar 2015, 16:59
von thaloss
Hallo,

ich habe ein problem mit dem dynamischen erstellen von Dictionaries aus einer for Schleife heraus.
es bleibt nur das jeweils letzte erzeugte Dictionary erhalten. Ein einfaches Beispiel sieht so aus:

Code: Alles auswählen

a = { 'a1' : '1',
      'a2' : '2'}
b = { 'b1' : '3',
      'b2' : '4'}
test = {}
anz = 3
test = a.copy()
print('copy of a')
print(test)
test['nb_of_BB'] = anz
for x in range(0, test['nb_of_BB']):
    test['BB'] = {}
    test['BB'][x] = b.copy()
    print('%i. copy of b' %(x+1))
    print(test)
    print(test['BB'].keys())
Der zugehörige Output sieht so aus:

Code: Alles auswählen

copy of a
{'a1': '1', 'a2': '2'}
1. copy of b
{'a1': '1', 'a2': '2', 'BB': {0: {'b2': '4', 'b1': '3'}}, 'nb_of_BB': 3}
dict_keys([0])
2. copy of b
{'a1': '1', 'a2': '2', 'BB': {1: {'b2': '4', 'b1': '3'}}, 'nb_of_BB': 3}
dict_keys([1])
3. copy of b
{'a1': '1', 'a2': '2', 'BB': {2: {'b2': '4', 'b1': '3'}}, 'nb_of_BB': 3}
dict_keys([2])
Wie man sieht werden die einzelnen Dictionaries erzeugt aber sie werden aus irgendeinem Grunde gelöscht.
hat jemand eine Idee warum ?
wenn ich die Einträge statisch erzeuge funktioniert es einwandfrei.

Code: Alles auswählen

a = { 'a1' : '1',
      'a2' : '2'}
b = { 'b1' : '3',
      'b2' : '4'}
test = {}
test = a.copy()
print('copy of a')
print(test)
test['nb_of_BB'] = 3
test['BB'] = {}
test['BB'][0] = b.copy()
print('1. copy of b')
print(test)
test['BB'][1] = b.copy()
print('2. copy of b')
print(test)
print(test['BB'].keys())
test['BB'][2] = b.copy()
print('3. copy of b')
print(test)
print(test['BB'].keys())
und der Output:

Code: Alles auswählen

copy of a
{'a2': '2', 'a1': '1'}
1. copy of b
{'nb_of_BB': 3, 'a2': '2', 'a1': '1', 'BB': {0: {'b2': '4', 'b1': '3'}}}
2. copy of b
{'nb_of_BB': 3, 'a2': '2', 'a1': '1', 'BB': {0: {'b2': '4', 'b1': '3'}, 1: {'b2': '4', 'b1': '3'}}}
dict_keys([0, 1])
3. copy of b
{'nb_of_BB': 3, 'a2': '2', 'a1': '1', 'BB': {0: {'b2': '4', 'b1': '3'}, 1: {'b2': '4', 'b1': '3'}, 2: {'b2': '4', 'b1': '3'}}}
dict_keys([0, 1, 2])
Vielen Dank

Re: dynamisches erstellen von dictionaries - Python Fehler?

Verfasst: Freitag 27. Februar 2015, 17:08
von EyDu
Dann überlege doch mal, was Zeile 12 in deinem ersten Code macht ;-)

Re: dynamisches erstellen von dictionaries - Python Fehler?

Verfasst: Freitag 27. Februar 2015, 17:11
von thaloss
Autsch, danke, die Zeile muß natürlich for das for und schon funktioniert es.
Betriebsblindheit ist alles ;)

Re: dynamisches erstellen von dictionaries - Python Fehler?

Verfasst: Freitag 27. Februar 2015, 17:38
von Sirius3
@thaloss: Was willst Du überhaupt erreichen? Soll das nur eine Spielerei sein, oder gibt es einen tieferen Sinn?
Wenn die Schlüssel eines Wörterbuchs aufsteigende Zahlen sind, dann wäre es naheliegender dafür eine Liste zu verwenden. Die Anzahl an BBs braucht man auch nicht zusätzlich in die Datenstruktur zu schreiben, weil sich die ja aus der Länge der Liste ganz leicht ermitteln läßt.