Python - 类似 PHP 的动态数组

标签 python django arrays multidimensional-array

我尝试在 python 中创建一个类似 PHP 的数组。在 Perl 中,它将是一个散列...

我想将数据库中的内容组织在一个数组中以便于访问。

类似于:

myarray[db_numeric_value1][db_numeric_value2] = db_str_value

但到目前为止还没有运气......

我尝试将 myarray 初始化为对象列表 (myarray = {}) 或数组 (myarray = [])...还尝试初始化“子数组”,但也不起作用...

感谢您的帮助!

罗恩

最佳答案

你可以使用Python的defaultdict和适当的“default_factory”:

from collections import defaultdict
myarray = defaultdict(dict)
myarray[4][5] = 34

关于Python - 类似 PHP 的动态数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10846524/

相关文章:

python - 为什么 Django 中没有 response_finished 信号?

c++ - 快速的每列求和。可能的?

javascript - 关于函数返回数组数据的问题

python - 如何将附加参数传递给自定义 python 排序函数

database - 在服务器上存储多维数组的最佳方式?

python - turtle.done() 在 Spyder 中不起作用

python - 与 Wordpress 一起部署 Django 应用程序(在 suburl)

python - MXNet 后向形状不一致

python - 将字符串列表转换为数字列表 Python

python - Django auth 中间件的一些内部结构