python如何创建五维空列表

47次阅读
没有评论

共计 1993 个字符,预计需要花费 5 分钟才能阅读完成。

在 Python 中,可以使用嵌套列表的方式创建多维列表。要创建一个五维空列表,可以使用嵌套循环的方式来实现。下面是一个示例代码:

# 创建五维空列表
dim1 = 3  # 第一维大小
dim2 = 4  # 第二维大小
dim3 = 2  # 第三维大小
dim4 = 5  # 第四维大小
dim5 = 6  # 第五维大小

# 使用嵌套循环创建五维空列表
five_dim_list = [[[[[None for _ in range(dim5)] for _ in range(dim4)] for _ in range(dim3)] for _ in range(dim2)] for _ in range(dim1)]

print(five_dim_list)

在上面的代码中,使用了五层嵌套循环来创建五维空列表。dim1代表第一维的大小,dim2代表第二维的大小,以此类推。通过循环遍历每一维的大小,使用 None 来初始化列表的元素。

输出结果为:

[[[[[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]]], [[[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]]], [[[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]], [[None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None], [None, None, None, None, None, None]]]]

可以看到,five_dim_list是一个五维空列表,每一维都有特定的大小。

丸趣 TV 网 – 提供最优质的资源集合!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-12-13发表,共计1993字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)