ValueError: setting an array element with a sequence in Python

Dung Do Tien Aug 12 2021 231

In Python 3.8.2, I want to create an array that has two dementions. You can see as below:

import numpy as np
array1 = [2.32, 6.888, 3.4, 9.01]
array2 = [67, 23, 99.80]
print(np.array([array1, array2],dtype = float))

But when I run I get an error ValueError: setting an array element with a sequence

Traceback (most recent call last):
  File "main.py", line 4, in <module>
    print(np.array([array1, array2],dtype = float))
ValueError: setting an array element with a sequence. The requested array has an inhomogeneous shape after 1 dimensions. The detected shape was (2,) + inhomogeneous part.

how can I resolve it? Thanks for any solution!!

Have 2 answer(s) found.
  • H

    Hieu Nguyen Aug 12 2021

    Two of your arrays have the same number of items. You can see array1 variable has 4 value items and array2 only has 3 value items.

    You can fix like that:

    import numpy as np
    array1 = [2.32, 6.888, 3.4]
    array2 = [67, 23, 99.80]
    print(np.array([array1, array2],dtype = float))

    happy code!!

  • M

    Muhammad Tayyab Aug 12 2021

    if my array is :

     example_array = [[1,2,3],[1,2]]

    Then I will get an error :

     ValueError: setting an array element with a sequence.

    but if I add padding then :

     example_array = [[1,2,3],[1,2,0]]

    Now it's working.

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close