本文介绍了阵列? (或者如果你愿意,可以列出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Pythonic列表是我想要的一维数组中的所有内容。 。 。

但是我正在尝试进行文本冒险并简化过程

创建一个网格,而不是这个房间的繁琐房间列表

连接到。


所以我想知道一个简单的方法来做一个SIMPLE二维数组。

Python的列表是一个在谈到如何做到这一点时有点混乱。

我意识到我可以做到


list = [[0] * N] * N


但是我不知道它是否会起作用因为我是新手并且只有理解数组才会理解数组,如果它们是网格状的。我还没有通过

线性代数,因为我的学校给了我一些背叛我是

已经不得不采取几何和allgebra II我的学校一个接一个地接受了这笔费用,所以任何建议或暗示

这个方向都没有用。


所以:

用Python做内部或外部SIMPLE数组的方法,

请。

解决方案



也许您想使用字典:


a = {}

a [(3,5)] = 2




不要追逐你的comp.lang.python,而是漫步

rec.arts.int-fiction for pointers选择的领域

特定的编程语言和用于写作的虚拟机

文本冒险。



你是对的怀疑那个结构。



[[0,0],[0,0]]



[[0,''糟糕。''],[0,''糟糕。'']]


问题是Python列表包含引用,而不是对象,



列表中乘法运算符的行为相结合。 br />



问题不在于构造,而在构建它的方式是

。只需一点一点地构建它,或者使用range()立即构建它们

然后在之后填充它。



[0,1],[0,1]]



[0,''确定。''],[0,1]]


一种灵活的方式可能是制作你的数据
$ b相反,$ b对象的属性。它不会像解码多维阵列一样快,但它会更容易使用。


然后再来一次's Inform,TADS,HUGO,Z-machine,Glk和

Adrift考虑。 ;-)


-

Neil Cerutti




< snip>



作为使用pyparsing的一个例子,我选择了一个简单的文本冒险

应用程序,并且必须创建一个二维网格的房间。演示

材料在,

源代码包含在pyparsing附带的示例中

Pythonic lists are everything I want in a one dimensional array . . .
but I''m trying to do a text adventure and simplify the proces by
creating a grid as opposed to a tedious list of rooms this room
connects to.

So I want to know a good way to do a SIMPLE two dimensional array.
Python''s lists are a little confusing when it comes to how to do this.
I realized that I could do

list = [ [0] * N ] * N

but I don''t know if it would work because I''m a newbie and only
understand arrays if they''re in grid-like form. And I haven''t ben thru
linear algebra yet, due to my school giving me a few set backs I''m
allready having to take geometry and allgebra II which are meant to be
taken one after another at my school, so any suggestions or hints in
that direction won''t be helpful.

So:
Way to do SIMPLE array, either internally or externally, with Python,
please.

解决方案

Maybe you want to use a dictionary:

a = {}
a[(3,5)] = 2


Not to chase you out of comp.lang.python, but take a stroll by
rec.arts.int-fiction for pointers to a selection of domain
specific programming languages and virtual machines for writing
text adventures.

You''re right to be suspicious of that construct.

[[0, 0], [0, 0]]

[[0, ''Oops.''], [0, ''Oops.'']]

The problem is that Python lists hold references, not objects,
combined with the behavior of the multiplication operator on
lists.

The problem wasn''t with the construct, but in the way it was
constructed. Just build it up bit by bit, or build it all at once
using range() and then fill it in afterwards.

[0, 1], [0, 1]]

[0, ''OK.''], [0, 1]]

A flexible way to do it instead might be to make your data
attributes of objects, instead. It won''t be as fast as decoding
multi-dimensional arrays, but it''ll be easier to work with.

Then again, there''s Inform, TADS, HUGO, the Z-machine, Glk and
Adrift to consider. ;-)

--
Neil Cerutti


<snip>

As an example of using pyparsing, I chose a simple text adventure
application, and had to create a 2-D grid of rooms. The presentation
materials are online at http://www.python.org/pycon/2006/papers/4/, and the
source code is included with the examples that ship with pyparsing


这篇关于阵列? (或者如果你愿意,可以列出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 15:52