How do I create character arrays in numpy?

后端 未结 3 865
[愿得一人]
[愿得一人] 2020-12-30 02:45

Say I have the following array:

import numpy as np
a = [\'hello\',\'snake\',\'plate\']

I want this to turn into a numpy array b

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-30 03:26

    Specify the string length as the shape parameter with unicode 1 char

    > string_array = ['..##.#..#.', '##..#.....', '#...##..#.', '####.#...#', '##.##.###.', '##...#.###', '.#.#.#..##', '..#....#..', '###...#.#.', '..###..###']
    > nummpy.array(string_array,dtype=('U1',10))
    array([['.', '.', '#', '#', '.', '#', '.', '.', '#', '.'],
           ['#', '#', '.', '.', '#', '.', '.', '.', '.', '.'],
           ['#', '.', '.', '.', '#', '#', '.', '.', '#', '.'],
           ['#', '#', '#', '#', '.', '#', '.', '.', '.', '#'],
           ['#', '#', '.', '#', '#', '.', '#', '#', '#', '.'],
           ['#', '#', '.', '.', '.', '#', '.', '#', '#', '#'],
           ['.', '#', '.', '#', '.', '#', '.', '.', '#', '#'],
           ['.', '.', '#', '.', '.', '.', '.', '#', '.', '.'],
           ['#', '#', '#', '.', '.', '.', '#', '.', '#', '.'],
           ['.', '.', '#', '#', '#', '.', '.', '#', '#', '#']], dtype='

提交回复
热议问题