简体   繁体   中英

How do I change a float number into an array so that I can concatenate it with a zeros function in Python?

I am extremely new to Python and I have tried searching for answers and debugging for days but I could not solve this issue. Please help me.

Main Question: How do I concatenate a zeros function, zeros((1,142)) with a float number, 1954.0 ?

Things I have tried:

  1. Converting the float number, 1954.0 into an array using: numpy.array(1954.0)
  2. Concatenate the zeros function with the converted float number: concatenate((zeros(1,142), numpy.array(1954.0)))

    [It gave a ValueError: all the input arrays must have same number of dimensions]

  3. I tried doing this instead: concatenate((zeros(1,142)), numpy.array([[1954.0]]))) and it gave a DIFFERENT ValueError message.

    [ValueError: all the input array dimensions except for the concatenation axis must match exactly]

  4. I have checked using: type(numpy.array(1954.0)) and it says <type 'numpy.ndarray'> and also checked using: type((zeros(1,142)) and it says <type 'numpy.ndarray'> . Both are of the same type, which means I should be able to concatenate but somehow it still gives me the various error messages shown in point 2 and 3.

I am desperately in need of help for this.

Nicer ways to accomplish this are

a = numpy.zeros((1,143))
a[0, -1] = 1954

or

a = numpy.append(numpy.zeros((1,142)), 1954)

Mike Graham's answer's are nicer but if you insist on using concatenate, then the following will work:

x = np.zeros( (1,143))
y = np.array([[1943]])
tot = np.concatenate((x,y), axis=1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM