如何在python中使用自定义填充创建Array2d

9gm1akwq  于 2023-10-14  发布在  Python
关注(0)|答案(1)|浏览(104)

我想创建一个自定义值,宽度和高度的Array2d。
我在网上搜过了,只找到一个数组,里面包含数字而不是字符串
例如,我想让它像这样:

fields_width = 5 # Width of the Fields we specified
fields_height = 5 # Height of the Fields we specified

# And this is the result
fields = [["A", "A", "A", "A", "A"],
          ["A", "B", "B", "B", "A"],
          ["A", "B", "B", "B", "A"],
          ["A", "B", "B", "B", "A"],
          ["A", "A", "A", "A", "A"]]

如何在Python中编写代码

mwg9r5ms

mwg9r5ms1#

你有一个列表,你想要一个2D数组。你可以使用NumPy来实现:

import numpy as np

# you need to tell numpy that it is an array of objects, otherwise 
# it uses fixed-size strings limited to the length of the longest
# element
s = np.array([['a','b'],['c','de doo doo doo']], dtype='object')

print(s[0,1]
# 'b'

相关问题