繁体   English   中英

Python列出了索引

[英]Python lists indexes

我有一个小问题,因为我刚接触python,我来自php和php我能够定义带有显式索引的数组

 array(
         "Index1" => array(),
         "Index2" => array()
         );

我想知道我是否能够使用python中的列表执行相同的操作,例如

["Index1" => [], "Index2" => []]

你在谈论python中的字典。 http://docs.python.org/2/library/stdtypes.html#dict

>>> d = {"Index1": [], "Index2": []}
>>> d["Index1"]
[]

要么

>>> d = dict(Index1=[], Index2=[])

等效字典是有序字典:

from collections import OrderedDict

d = OrderedDict([
    ('Index1', []),
    ('Index2', [])
])

尽管很少使用订单,但在大多数情况下,常规字典应该没问题:

d = {
    'Index1': [],
    'Index2': []
}

暂无
暂无

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

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