Convert string to numpy array

后端 未结 3 1208
死守一世寂寞
死守一世寂寞 2021-01-01 13:35

I have a string like mystr = \"100110\" (the real size is much bigger) I want to convert it to numpy array like mynumpy = [1, 0, 0, 1, 1, 0], mynumpy.shap

3条回答
  •  萌比男神i
    2021-01-01 14:37

    list may help you do that.

    import numpy as np
    
    mystr = "100110"
    print np.array(list(mystr))
    # ['1' '0' '0' '1' '1' '0']
    

    If you want to get numbers instead of string:

    print np.array(list(mystr), dtype=int)
    # [1 0 0 1 1 0]
    

提交回复
热议问题