简体   繁体   English

对于小型游戏,是否存在用于2D阵列的Python模块/配方(非numpy)

[英]Is there a Python module/recipe (not numpy) for 2d arrays for small games

I am writing some small games in Python with Pygame & Pyglet as hobby projects. 我正在用Pygame和Pyglet编写一些Python小游戏作为业余爱好项目。

A class for 2D array would be very handy. 2D数组的类非常方便。 I use py2exe to send the games to relatives/friends and numpy is just too big and most of it's features are unnecessary for my requirements. 我使用py2exe将游戏发送给亲戚/朋友,numpy太大了,大部分功能对我的要求都是不必要的。

Could you suggest a Python module/recipe I could use for this. 你能推荐一个我可以使用的Python模块/配方吗?

-- Chirag - 奇拉格

[Edit]: List of lists would be usable as mentioned below by MatrixFrog and zvoase. [编辑]:列表列表可用于MatrixFrog和zvoase,如下所述。 But it is pretty primitive. 但它非常原始。 A class with methods to insert/delete rows and columns as well as to rotate/flip the array would make it very easy and reusable too. 具有插入/删除行和列以及旋转/翻转数组的方法的类将使其非常容易和可重用。 dicts are good for sparse arrays only. dicts仅适用于稀疏数组。

Thank you for your ideas. 谢谢你的想法。

How about using a defaultdict? 如何使用defaultdict?

>>> import collections
>>> Matrix = lambda: collections.defaultdict(int)
>>> m = Matrix()
>>> m[3,2] = 6
>>> print m[3,4]   # deliberate typo :-)
0
>>> m[3,2] += 4
>>> print m[3,2]
10
>>> print m
defaultdict(<type 'int'>, {(3, 2): 10, (3, 4): 0})

As the underlying dict uses tuples as keys, this supports 1D, 2D, 3D, ... matrices. 由于底层字典使用元组作为键,因此支持1D,2D,3D,...矩阵。

The simplest approach would just be to use nested lists: 最简单的方法就是使用嵌套列表:

>>> matrix = [[0] * num_cols] * num_rows
>>> matrix[i][j] = 'value' # row i, column j, value 'value'
>>> print repr(matrix[i][j])
'value'

Alternatively, if you're going to be dealing with sparse matrices (ie matrices with a lot of empty or zero values), it might be more efficient to use nested dictionaries. 或者,如果您要处理稀疏矩阵(即具有大量空值或零值的矩阵),则使用嵌套字典可能更有效。 In this case, you could implement setter and getter functions which will operate on a matrix, like so: 在这种情况下,您可以实现将在矩阵上运行的setter和getter函数,如下所示:

def get_element(mat, i, j, default=None):
    # This will also set the accessed row to a dictionary.
    row = mat.setdefault(i, {})
    return row.setdefault(j, default)

def set_element(mat, i, j, value):
    row = mat.setdefault(i, {})
    row[j] = value

And then you would use them like this: 然后你会像这样使用它们:

>>> matrix = {}
>>> set_element(matrix, 2, 3, 'value') # row 2, column 3, value 'value'
>>> print matrix
{2: {3: 'value'}}
>>> print repr(get_element(matrix, 2, 3))
'value'

If you wanted, you could implement a Matrix class which implemented these methods, but that might be overkill: 如果你愿意,你可以实现一个实现这些方法的Matrix类,但这可能有点过分:

class Matrix(object):
    def __init__(self, initmat=None, default=0):
        if initmat is None: initmat = {}
        self._mat = initmat
        self._default = default
    def __getitem__(self, pos):
        i, j = pos
        return self._mat.setdefault(i, {}).setdefault(j, self._default)  
    def __setitem__(self, pos, value):
        i, j = pos
        self._mat.setdefault(i, {})[j] = value
    def __repr__(self):
        return 'Matrix(%r, %r)' % (self._mat, self._default)

>>> m = Matrix()
>>> m[2,3] = 'value'
>>> print m[2,3]
'value'
>>> m
Matrix({2: {3: 'value'}}, 0)

也许pyeuclid符合您的需求 - (过时但可用)格式化的文档在这里 ,ReST格式的最新文档在pyeuclid源文本文件中(要做自己的ReST文本格式,使用docutils )。

I wrote the class. 我写了这堂课。 Don't know if it is a good or redundant but... Posted it here http://bitbucket.org/pieceofpeace/container2d/ 不知道它是好还是多余......但在这里发表http://bitbucket.org/pieceofpeace/container2d/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM