numpy en dizi homogeneous, meaning it is a multidimensional table of elements all of the same type olduğu için hata alıyorum uzunluğu 16 dizeleri, (a) bir dizi yapar. Bu liste halinde farklı türdeki nesneleri olabilir "normal" Python, bir çok boyutlu liste-of-the listelerinden farklıdır.
Düzenli Python:
>>> CoverageACol = [[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]]
>>> CoverageACol[0][0] = "hello"
>>> CoverageACol
[['hello', 1, 2, 3, 4],
[5, 6, 7, 8, 9]]
NumPy:
>>> from numpy import *
>>> CoverageACol = arange(10).reshape(2,5)
>>> CoverageACol
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
>>> CoverageACol[0,0] = "Hello"
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/biogeek/<ipython console> in <module>()
ValueError: setting an array element with a sequence.
Yani, elde etmek istediğinize bağlıdır, neden dolu bir dizide bir dize depolamak ister misin sayılarla kalanı için? Bu gerçekten ne istediğinizi ise, dizeye NumPy dizinin veri türü ayarlayabilirsiniz: Sadece Hello
ilk harfi atanır
>>> CoverageACol = array(range(10), dtype=str).reshape(2,5)
>>> CoverageACol
array([['0', '1', '2', '3', '4'],
['5', '6', '7', '8', '9']],
dtype='|S1')
>>> CoverageACol[0,0] = "Hello"
>>> CoverageACol
array([['H', '1', '2', '3', '4'],
['5', '6', '7', '8', '9']],
dtype='|S1')
Bildirimi. Eğer tüm sözcük atanan almak istiyorsanız, an array-protocol type string ayarlamanız gerekir:
>>> CoverageACol = array(range(10), dtype='a5').reshape(2,5)
>>> CoverageACol:
array([['0', '1', '2', '3', '4'],
['5', '6', '7', '8', '9']],
dtype='|S5')
>>> CoverageACol[0,0] = "Hello"
>>> CoverageACol
array([['Hello', '1', '2', '3', '4'],
['5', '6', '7', '8', '9']],
dtype='|S5')
detaylı açıklama için teşekkür ederiz! – Moose
Ayar 'd_type = Nesnesi' da çalışır: https://stackoverflow.com/questions/14639496/python-numpy-array-of-arbitrary-length-strings –
sizin doğrultusunda' overageACol = array (aralık (10), d_type = str) .reshape (2,5) '. list 'ya 'dict'' için 'dtype' değiştirmek mümkün mü? –