2017-12-16 2 views
0

Ich ziehe mir die Haare darüber. Ich versuche, die Elemente eines numpy Array ohne Erfolg zu ändern:numpy Array-Element ändert seinen Wert nicht, wenn ihm ein Wert zugewiesen wird

import numpy as np 
c = np.empty((1), dtype='i4, S, S, S, S, S, S, S, S, S') 
print(c) 
c[0][1]="hello" 
c[0][2]='hello' 
c[0][3]=b'hello' 
print(c) 

Ausgang:

[(0, b'', b'', b'', b'', b'', b'', b'', b'', b'')] 
[(0, b'', b'', b'', b'', b'', b'', b'', b'', b'')] 
+0

Was sind Sie mit diesem 'dype' tun? –

+0

Das Array sollte Tupel (10) pro Zeile enthalten. In diesem Fall 1 Reihe. Ich habe dieses Beispiel von einer anonymen Seite ausgewählt. https://docs.scipy.org/doc/numpy-1.10.1/user/basics.rec.html - 'x = np.zeros (3, dtype = '3int8, float32, (2,3) float64') ' – Krischu

Antwort

1

Strings werden Länge in numpy fixiert. Was passt nicht einfach verworfen:

np.dtype('S').itemsize 
# 0 

so die Zuordnung zu der an Position Saiten abgeschnitten wird 0:

np.array('hello', dtype='S4') 
# array(b'hell', dtype='|S4') 

dtype('S') zu dtype('S0') als gleichwertig erscheint.

Wenn Sie wissen, die maximale Länge im Voraus zu erwarten:

c = np.empty((1,), dtype=', '.join(['i4'] + 9*['S5'])) 
for i in range(1, 10): 
    c[0][i] = 'hello' 

c 
# array([ (-1710610776, b'hello', b'hello', b'hello', b'hello', b'hello', b'hello', b'hello', b'hello', b'hello')], 
# dtype=[('f0', '<i4'), ('f1', 'S5'), ('f2', 'S5'), ('f3', 'S5'), ('f4', 'S5'), ('f5', 'S5'), ('f6', 'S5'), ('f7', 'S5'), ('f8', 'S5'), ('f9', 'S5')]) 

Wenn Sie benötigen flexible Länge Sie Objekt dtype verwenden können:

c = np.empty((1,), dtype=', '.join(['i4'] + 9*['O'])) 
for i in range(1, 10): 
    c[0][i] = 'hello world'[:i] 

c 
# array([ (0, 'h', 'he', 'hel', 'hell', 'hello', 'hello ', 'hello w', 'hello wo', 'hello wor')], 
# dtype=[('f0', '<i4'), ('f1', 'O'), ('f2', 'O'), ('f3', 'O'), ('f4', 'O'), ('f5', 'O'), ('f6', 'O'), ('f7', 'O'), ('f8', 'O'), ('f9', 'O')]) 

Wenn Sie feste Länge wollen gerade groß genug ist, haben alle Datensätze zur Hand und sind nicht zu wählerisch über die genauen Typen, die Sie für Sie haben können:

lot = [(5,) + tuple('hello world 2 3 4 5 6 7 8 9'.split()), (8,) + tuple('0 1 2 3 short loooooooong 6 7 8 9'.split())] 
lot 
# [(5, 'hello', 'world', '2', '3', '4', '5', '6', '7', '8', '9'), (8, '0', '1', '2', '3', 'short', 'loooooooong', '6', '7', '8', '9')] 
c = np.rec.fromrecords(lot) 
c 
# rec.array([(5, 'hello', 'world', '2', '3', '4', '5', '6', '7', '8', '9'), 
#  (8, '0', '1', '2', '3', 'short', 'loooooooong', '6', '7', '8', '9')], 
#  dtype=[('f0', '<i8'), ('f1', '<U5'), ('f2', '<U5'), ('f3', '<U1'), ('f4', '<U1'), ('f5', '<U5'), ('f6', '<U11'), ('f7', '<U1'), ('f8', '<U1'), ('f9', '<U1'), ('f10', '<U1')]) 
0

Sie verwenden Strings der Länge 0. Sie haben die Felder groß genug für Ihren Text zu machen:

import numpy as np 
c = np.empty((1), dtype='i4, S5, S5, S5, S5, S5, S5, S5, S5, S5') 
print(c) 
c[0][1]="hello" 
c[0][2]='hello' 
c[0][3]=b'hello' 
print(c) 
Verwandte Themen