Python Array with String Indices

后端 未结 2 1478
半阙折子戏
半阙折子戏 2020-12-07 22:17

Is it possible to use strings as indices in an array in python?

For example:

myArray = []
myArray[\"john\"] = \"johns value\"
myArray[\"jeff\"] = \"j         


        
2条回答
  •  盖世英雄少女心
    2020-12-07 23:06

    Even better, try an OrderedDict (assuming you want something like a list). Closer to a list than a regular dict since the keys have an order just like list elements have an order. With a regular dict, the keys have an arbitrary order.

    Note that this is available in Python 3 and 2.7. If you want to use with an earlier version of Python you can find installable modules to do that.

提交回复
热议问题