如何在Python中创建带有自定义填充的Array2d

问题描述 投票:0回答:1

我想创建一个具有自定义值、宽度和高度的 Array2d。

我在互联网上搜索过,但只找到了一个包含数字而不是字符串的数组

比如我想做这样的:

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 multidimensional-array
1个回答
0
投票

你有一个列表,你想要一个二维数组。您可以使用 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'
© www.soinside.com 2019 - 2024. All rights reserved.