Immer mal wieder... Ich habe ein zweidimensionales Array, eigentlich ein verschachteltes Dict[y][x]. Daraus möchte ich alle Werte (Objekte) einer Spalte ziehen. Es sind aber nicht alle Felder belegt, das soll als None wiedergegeben werden. Geht das kürzer/effektiver als: def getCol(array, col): result = [] for row in array: if col in self[row]: result.append(self[row][col]) else: result.append(None) return result Ich dachte an sowas: return [ array[row][col] for row in array if col in array[row] else None ] aber das mit dem else geht ja nicht... Grüße vom Südsee! Henning Hraban Ramm Südkurier Medienhaus / MediaPro Systembetreuung / Systementwicklung _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Henning.Ramm@mediapro-gmbh.de:
Ich dachte an sowas:
return [ array[row][col] for row in array if col in array[row] else None ]
if col in array.keys() Bin nicht sicher, ob das geht. J. -- In this bunker there are women and children. There are no weapons. [Agree] [Disagree] <http://www.slowlydownward.com/NODATA/data_enter2.html> _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
Am Donnerstag, 18. August 2005 00:51 schrieb Henning.Ramm@mediapro-gmbh.de:
Ich habe ein zweidimensionales Array, eigentlich ein verschachteltes Dict[y][x]. Daraus möchte ich alle Werte (Objekte) einer Spalte ziehen. Es sind aber nicht alle Felder belegt, das soll als None wiedergegeben werden. Geht das kürzer/effektiver als:
def getCol(array, col): result = [] for row in array: if col in self[row]: result.append(self[row][col]) else: result.append(None) return result
Ich dachte an sowas:
return [ array[row][col] for row in array if col in array[row] else None ]
aber das mit dem else geht ja nicht...
Ungetestet: [row.get(col) for row in array.itervalues()] Der Haken ist, dass die Reihenfolge der Einträge in einem dict undefiniert ist. Und was passiert, wenn in dem 2D array eine ganze Zeile fehlt? Peter _______________________________________________ python-de maillist - python-de@python.net http://python.net/mailman/listinfo/python-de
participants (3)
-
Henning.Ramm@mediapro-gmbh.de
-
Jochen Schulz
-
Peter Otten