Python 中的二维数组 [英] 2D arrays in Python

查看:36
本文介绍了Python 中的二维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Python 中创建二维数组的最佳方法是什么?

What's the best way to create 2D arrays in Python?

我想要的是存储这样的值:

What I want is want is to store values like this:

X , Y , Z

以便我访问像 X[2],Y[2],Z[2]X[n],Y[n],Z[n] 之类的数据code> 其中 n 是可变的.一开始我不知道 n 有多大,所以我想在最后附加值.

so that I access data like X[2],Y[2],Z[2] or X[n],Y[n],Z[n] where n is variable. I don't know in the beginning how big n would be so I would like to append values at the end.

推荐答案

>>> a = []

>>> for i in xrange(3):
...     a.append([])
...     for j in xrange(3):
...             a[i].append(i+j)
...
>>> a
[[0, 1, 2], [1, 2, 3], [2, 3, 4]]
>>>

这篇关于Python 中的二维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆