Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的多维数组_Python_Python 3.x - Fatal编程技术网

python中的多维数组

python中的多维数组,python,python-3.x,Python,Python 3.x,在php中,我们可以创建多维关联数组,如 $item = array( "spec" => array( "more" => "smth" )) 并通过foreach($a=>b)迭代它们 我想知道我是否可以做同样的事情,包括python中的迭代过程是的,您可以使用这样的字典(一组字典)(python 3): 听起来你想要字典。您可以以类似的方式迭代它们(也在链接中进行了解释)。虽然dict.items()将实现所需的功能,但在dict上迭

在php中,我们可以创建多维关联数组,如

 $item = array(
     "spec" => array(
        "more" => "smth"
    ))   
并通过
foreach($a=>b)迭代它们


我想知道我是否可以做同样的事情,包括python中的迭代过程是的,您可以使用这样的字典(一组字典)(python 3):


听起来你想要字典。您可以以类似的方式迭代它们(也在链接中进行了解释)。虽然
dict.items()
将实现所需的功能,但在
dict
上迭代并不是pythonic方式
dict.items()
返回字典的副本;您实际想要使用的是dict.iteritems()
;这将返回一个迭代器。@Adam Mitchell否,因为Python 3 dict.items()与Python 2中的旧dict.iteritems()具有相同的行为。这就是我说这个例子是针对Python3的原因之一。在Python3.6+中,您也可以使用f字符串,例如,
f'键是{key},值是{value}
item = {"spec": {"more": "smth"}}

for key, value in item.items():
    print("The key was '" + key + "' and the value was:")
    print(value)