将现有数组转换为多维数组

时间:2016-02-27 18:33:30

标签: python multidimensional-array

我有一个简单的数组,我将字符串存储在。

e.g。

item['name'] = 'suchy'
item['score'] = 'such'

然而,现在,在游戏的后期我想为这个项目添加一个数组。

在我的情况下,isn" item []已经是一个数组和' name'和'得分'他们是钥匙吗?我存储了这样的'和'和#39;作为变量,但我不想存储字符串,而是要存储更多数组。

我略高于愚蠢,但这对我来说似乎很复杂。如何用python完成?

修改

道歉。

在PHP中,您可以执行类似

的操作
$myArray = array
{
    'suchy' => 'such',
    'anotherarray' => array { 'something', 'else'}
}

稍后,在数组中添加内容相当容易。

 $myArray[0][3] = 'etc'

我试图找出如何使用python做类似的事情。感谢您的评论到目前为止。已经学到了一些东西。谢谢!

2 个答案:

答案 0 :(得分:1)

如果我理解正确你需要这样的东西

   services.AddEntityFramework()
           .AddSqlServer()
           .AddDbContext<ProductDbContext>();

比你以后的游戏中你可以像这样使用它

item = {}
item['somekey'] = ['suchy'] # you're indicate that in dictionary item you will store list so in this lit you can put anything you want later and list too
item['score'] = ['such']

或者如果要在词典中添加值等于数组的新项目 你可以写这个

item['somekey'].append([1,11,2,3,4])

答案 1 :(得分:1)

在Python中,这个:

$myArray = array
{
    'suchy' => 'such',
    'anotherarray' => array { 'something', 'else'}
}

就是这样:

my_dict = {
    'suchy': 'such',
    'anotherarray': ['something', 'else']
}

如果您想在第一级添加内容,只需:

my_dict['stuff'] = [1,2,3,4]

现在将成为:

my_dict = {
    'suchy': 'such',
    'anotherarray': ['something', 'else']
    'stuff': [1, 2, 3, 4]
}

如果您想更新列表,请说明存储在&#39; anotherarray&#39;中的列表,您可以这样做:

my_dict['anotherarray'].append('things')

my_dict['anotherarray']

的输出
['something', 'else', 'things']

我建议在Python中阅读有关dictionaries的教程:

Documentation from official docs

相关问题