tags:

views:

989

answers:

9

In PHP I can name my array indicies so that I may have something like:
$shows = Array(0 => Array('id' => 1, 'name' => 'Sesaeme Street'), 1 => Array('id' => 2, 'name' => 'Dora The Explorer'));

Is this possible in Python?

+3  A: 

Yes,

a = {"id": 1, "name":"Sesame Street"}
Lou Franco
+17  A: 

This sounds like the PHP array using named indices is very similar to a python dict:

shows = [
  {"id": 1, "name": "Sesaeme Street"},
  {"id": 2, "name": "Dora The Explorer"},
]

See http://docs.python.org/tutorial/datastructures.html#dictionaries for more on this.

Michael Twomey
+1: I was halfway through typing the same thing!
S.Lott
Bit too complicated. I think the poster wanted dicts
Rory
+8  A: 

PHP arrays are actually maps, which is equivalent to dicts in Python.

Thus, this is the Python equivalent:

showlist = [{'id':1, 'name':'Sesaeme Street'}, {'id':2, 'name':'Dora the Explorer'}]

Sorting example:

from operator import attrgetter

showlist.sort(key=attrgetter('id'))

BUT! With the example you provided, a simpler datastructure would be better:

shows = {1: 'Sesaeme Street', 2:'Dora the Explorer'}

Deestan
+2  A: 

You should read the python tutorial and esp. the section about datastructures which also covers dictionaries.

André
+3  A: 

To assist future Googling, these are usually called associative arrays in PHP, and dictionaries in Python.

Steve Klabnik
+1  A: 

Not exactly the same syntax, but there are a number of dictionary extensions out there which respect the order in which the key/value pairs have been added. E.g. seqdict.

fivebells
+5  A: 

@Unkwntech,

What you want is available in the just-released Python 2.6 in the form of named tuples. They allow you to do this:

import collections
person = collections.namedtuple('Person', 'id name age')

me = person(id=1, age=1e15, name='Dan')
you = person(2, 'Somebody', 31.4159)

assert me.age == me[2]   # can access fields by either name or position
Dan
Of course, this can be simulated for older versions of Python (example: http://users.forthnet.gr/ath/chrisgeorgiou/python/TupleStruct.py )
ΤΖΩΤΖΙΟΥ
Thats good to know about 2.6, looks like its time to upgrade.
Unkwntech
Named tuples, or tuples in general, are **immutable**.
Sridhar Ratnakumar
A: 

Python has lists and dicts as 2 separate data structures. PHP mixes both into one. You should use dicts in this case.

Rory
A: 

I did it like this:

def MyStruct(item1=0, item2=0, item3=0):
    """Return a new Position tuple."""
    class MyStruct(tuple):
        @property
        def item1(self):
            return self[0]
        @property
        def item2(self):
            return self[1]
        @property
        def item3(self):
            return self[2]
    try:
        # case where first argument a 3-tuple                               
        return MyStruct(item1)
    except:
        return MyStruct((item1, item2, item3))

I did it also a bit more complicate with list instead of tuple, but I had override the setter as well as the getter.

Anyways this allows:

    a = MyStruct(1,2,3)
    print a[0]==a.item1