What is the best way to create a string array in python?

后端 未结 11 1413
南旧
南旧 2021-02-03 17:45

I\'m relatively new to Python and it\'s libraries and I was wondering how I might create a string array with a preset size. It\'s easy in java but I was wondering how I might do

11条回答
  •  难免孤独
    2021-02-03 18:24

    Sometimes I need a empty char array. You cannot do "np.empty(size)" because error will be reported if you fill in char later. Then I usually do something quite clumsy but it is still one way to do it:

    # Suppose you want a size N char array
    charlist = [' ']*N # other preset character is fine as well, like 'x'
    chararray = np.array(charlist)
    # Then you change the content of the array
    chararray[somecondition1] = 'a'
    chararray[somecondition2] = 'b'
    

    The bad part of this is that your array has default values (if you forget to change them).

提交回复
热议问题