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

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

问题描述

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

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

  X,Y,Z

让我访问诸如数据X [2],Y [2],Z [2] X [N],Y [ N],Z [N] ,其中 N 是可变的。
我不知道在开始的时候有多大 N 会,所以我想在最后追加值。


解决方案

 >>>一个= []>>>对于我的xrange(3):
... a.append([])
......为的xrangeĴ(3):
一... [I] .append(I + J)
...
>>>一个
[0,1,2],[1,2,3],[2,3,4]
>>>

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

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天全站免登陆