Seite 1 von 1

Umsortieren von Array

Verfasst: Dienstag 30. Oktober 2012, 14:51
von 01detlef
Hallo zusammen,

ich möchte gern ein Array umordnen und mir fehlt eine Idee. Zurzeit sieht das Array ao aus:

x11 x21 x31
y11 y21 y31
z11 z21 y31
x12 x22 x32
y12 y22 y32
z12 z22 z32

so soll es sein:

x11 y11 z11
x12 y12 z12

x21 y21 z21
x22 y22 z22

x31 y31 z31
x32 y32 z32

Hat da jemand eine, wie man das geschickt machen kann?

detlef

Re: Umsortieren von Array

Verfasst: Dienstag 30. Oktober 2012, 15:05
von pillmuncher
itertools ist dein Freund:

Code: Alles auswählen

>>> items = [
...     ['x11', 'x21', 'x31'],
...     ['y11', 'y21', 'y31'],
...     ['z11', 'z21', 'y31'],
...     ['x12', 'x22', 'x32'],
...     ['y12', 'y22', 'y32'],
...     ['z12', 'z22', 'z32']]
>>> from itertools import chain, izip
>>> from pprint import pprint
>>> pprint(map(list, chain.from_iterable((s[:3], s[3:]) for s in izip(*items))))
[['x11', 'y11', 'z11'],
 ['x12', 'y12', 'z12'],
 ['x21', 'y21', 'z21'],
 ['x22', 'y22', 'z22'],
 ['x31', 'y31', 'y31'],
 ['x32', 'y32', 'z32']]
edit: vemutlich meinst du Listen, nicht Arrays.

Re: Umsortieren von Array

Verfasst: Dienstag 30. Oktober 2012, 15:28
von 01detlef
Ich habe das ganze aber als numpy.array? Dadurch kann ich keine Listen verwenden oder sollte ich das erst in eine Liste umwandeln?

danke
detlef

Re: Umsortieren von Array

Verfasst: Dienstag 30. Oktober 2012, 16:22
von 01detlef
Hallo vielen Dank, ich habe es hinbekommen. Nun noch eine Frage dazu:

WIe kann ich nun am Besten sagen, dass die ersten beiden Zeilen + den drei Spalten in einer Matrix gespeichert werden sollen, dann die dritte-vierte Zeile + Spalten usw.? Ich möchte dann nämlich die "Matrizenteile" mit einer anderen Matrix multiplizieren?

detlef

Re: Umsortieren von Array

Verfasst: Dienstag 30. Oktober 2012, 20:40
von pillmuncher
Meinst du so?

Code: Alles auswählen

>>> import numpy as np
>>>
>>> items = np.array([
...     ['x11', 'x21', 'x31'],
...     ['y11', 'y21', 'y31'],
...     ['z11', 'z21', 'y31'],
...     ['x12', 'x22', 'x32'],
...     ['y12', 'y22', 'y32'],
...     ['z12', 'z22', 'z32'],
... ])
>>>
>>> print np.expand_dims(items.transpose(), 1).reshape(3, 2, 3)
[[['x11' 'y11' 'z11']
  ['x12' 'y12' 'z12']]

 [['x21' 'y21' 'z21']
  ['x22' 'y22' 'z22']]

 [['x31' 'y31' 'y31']
  ['x32' 'y32' 'z32']]]