Bei der Zuweisung eines Python-String-Format Rückgabewert wie '67.67.%s.%s' % (str(j), str(i))
, ist es inkonsistent zwischen Zuordnung zu einem Schlüssel eines Diktats und Zuweisung zu einem Schlüssel in einem Unter-Diktat eines Diktats.Python Zuweisung String-Format Rückgabewert Verhalten ist inkonsistent
Zum Beispiel:
i = 0
j = 1
dict_template = {"id": "1", "another_dict": {"value": "hello"}}
dict_list = []
for idx in list(range(2, 1002)):
new_dict = copy.copy(dict_template)
new_dict['id'] = 'obj_id_%s' % str(idx)
new_dict['another_dict']['value'] = '67.67.%s.%s' % (str(j), str(i))
dict_list.append(new_dict)
if i < 254:
i += 1
else:
j += 1
i = 1
In diesem Beispiel jeder new_dict [ 'andere'] [ 'value'] die gleiche Zeichenfolge sein würde, und hat die gleiche ID. Wenn ich jedoch diese Zeile new_dict['another_dict']['value'] = '67.67.%s.%s' % (str(j), str(i))
zu new_dict["another_dict"] = '67.67.%s.%s' % (str(j), str(i))
ändere, würde jedes new_dict ["another_dict"] andere Werte haben.
Übrigens benutze ich Python 3.4.3.