Python - Convert float to string

1

I have a matrix of floats of 3000 by 17, the theme is that in the first column I want to save strings.

With the following code:

tabla = numpy.empty((3000, 17))

nombres = ['i0{i}_0{d}_{n}.bmp'.format(i = imagen, d = distorsion, n = 
nivel) for imagen in range(1,26) for distorsion in range(1,25) for 
nivel in range(1,6)]

tabla[:, 0] = nombres

Obviously python gives the following error:

ValueError: could not convert string to float: 'i01_01_1.bmp'

But I do not want to convert the strings to float, but save strings in that column, in the rest of the matrix I will have float values.

I hope you can help me.

Thank you very much already.

Greetings.

Lucia

    
asked by Lucy_in_the_sky_with_diamonds 10.04.2017 в 20:52
source

2 answers

0

you have a two-dimensional array

a=[[1,2,3],[2,3,4],[3,4,5]]

you have a one-dimensional array

b=['bar','foo','car']

and you change the first value of each one by the value in b

for x in a[:]:
    x[0]=b[a.index(x)]

[['bar', 2, 3], ['foo', 3, 4], ['car', 4, 5]]

applied in your example

a=tabla.tolist()
for x in a[:]:
        x[0]=nombres[a.index(x)]
print a
    
answered by 10.04.2017 / 21:52
source
0

Use dtype = object. I think with this it works:

tabla = numpy.empty((3000, 17), dtype=object)

If you want to use numpy and mix floats with strings you need to use structured matrices. See here link or here link

    
answered by 10.04.2017 в 21:17