如何制作一系列单词?蟒蛇

时间:2017-06-22 21:14:26

标签: arrays words

下午好,我想知道如何制作一系列字母,例如我有一个字母表列表:

alphabet=[chr(i) for i in range(ord('a'),ord('z')+1)]

我想用这个列表做一个5 * 5的矩阵,我已经完成了,就是这个,但是python给了我错误

dimension=5
A= np.zeros((dimension,dimension))
n=0
for j in range(dimension):
    for i in range(dimension):
        A[i][j] = alphabet[n]
        n=n+1 

给我的错误是:

Traceback (most recent call last):
  File "Cuestionario 4.py", line 217, in <module>
    A[i][j] = alphabet[n]
ValueError: could not convert string to float: 'a'

感谢您的关注

1 个答案:

答案 0 :(得分:0)

替换此

A= np.zeros((dimension,dimension))

A= np.chararray((dimension, dimension))

完整代码应如下所示

dimension=5
A= np.chararray((dimension,dimension))
n=0
for j in range(dimension):
    for i in range(dimension):
        A[i][j] = alphabet[n]
        n=n+1