Gleicher Value, neuer Key; alten Key löschen
Verfasst: Montag 3. November 2008, 12:12
Code: Alles auswählen
q = {0: 'foo'}
q[1] = q[0]
del(q[0])
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
q = {0: 'foo'}
q[1] = q[0]
del(q[0])
__marcus__ hat geschrieben:Ist es möglich einem Value einen neuen Key zuzuweisen oder geht das nur genau so?Code: Alles auswählen
q = {0: 'foo'} q[1] = q[0] del(q[0])
Code: Alles auswählen
q.update({1:q.pop(0)})
Ist das nicht nur ein anderer Umweg zum selben Ziel?Qubit hat geschrieben:Code: Alles auswählen
q.update({1:q.pop(0)})
Code: Alles auswählen
q[0] = q.pop(1)
The dictionary p should not be mutated during iteration. It is safe (since Python 2.1) to modify the values of the keys as you iterate over the dictionary, but only so long as the set of keys does not change. For example:
Code: Alles auswählen
PyObject *key, *value;
Py_ssize_t pos = 0;
while (PyDict_Next(self->dict, &pos, &key, &value)) {
int i = PyInt_AS_LONG(value) + 1;
PyObject *o = PyInt_FromLong(i);
if (o == NULL)
return -1;
if (PyDict_SetItem(self->dict, key, o) < 0) {
Py_DECREF(o);
return -1;
}
Py_DECREF(o);
}
Ist dafür aber die schönste LösungHWK hat geschrieben:Edit: zu spät