-3

私はpython 2.7.5でこのコードを持っていました:

#!/usr/bin/env python2
#Ejercicio 24

from numpy import *
from string import *

def main():
i=0
j=0
k=0
filas=5
temp=''
columnas=3
nombres=['Julio' , 'Andres', 'Cesar', 'Maria', 'Isabel']
print nombres
tabla= arange(15)
tabla=tabla.reshape(filas,columnas)
print tabla

for j in range(columnas):
    for i in range(filas):
        if j==0:
            temp=nombres[i]
            #print temp
                    tabla[j,i]=int(float32(temp))
            print tabla[j,i]


return 0


if __name__ == '__main__':
main()

文字列(名前)を含むリストがありますが、これらの名前をタブラという配列の最初の列に割り当てたいのですが、コンパイラで次のエラーが発生します:

['Julio', 'Andres', 'Cesar', 'Maria', 'Isabel']
[[ 0  1  2]
[ 3  4  5]
[ 6  7  8]
[ 9 10 11]
[12 13 14]]
Julio
Traceback (most recent call last):
File "ejercicio24.py", line 34, in <module>
 main()
File "ejercicio24.py", line 25, in main
tabla[j,i]=int(float32(temp))
ValueError: could not convert string to float: Julio

配列の特定の列に文字列を割り当てることはできますか?

4

2 に答える 2

0

しかし、「Julio」を float32 に変換するにはどうすればよいと思いますか? 次のように入力するだけです。

if j==0:
    temp=nombres[i]
    #print temp
    tabla[j,i]=temp
    print tabla[j,i]
于 2013-11-10T19:05:58.140 に答える