is this data structure build-in or I'll have to write my own class?

Jorge Vargas jorge.vargas at gmail.com
Wed Feb 20 09:05:47 EST 2008


dict doesn't maintains order.

>>> a=[(3,2),(2,2)]
>>> dict(a).items()
[(2, 2), (3, 2)]
>>> a=[(3,2),(2,2)]
>>> assert dict(a).items()==a
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AssertionError
>>> dict(a).items()
[(2, 2), (3, 2)]


On Feb 20, 2008 7:50 AM, subeen <tamim.shahriar at gmail.com> wrote:
> I think you should go for 'dictionary' that is a built-in data
> structure of Python.
>
>
> regards,
> Subeen
> http://love-python.blogspot.com/
>
>
> On Feb 20, 7:32 pm, "Jorge Vargas" <jorge.var... at gmail.com> wrote:
> > 2008/2/20 Jarek Zgoda <jzg... at o2.usun.pl>:> Jorge Vargas napisa³(a):
> >
> > > > - attribute access (or index)
> > > > - maintain the order (for iter and print)
> > > > - be mutable.
> >
> > > These are all attributes of standard Python lists.
> >
> > probably I confused you with the "or index" part. I want to be able to
> > do item.value1 or item['value1'] the list can't do this.
> >
> > > > in case there isn't one. I was thinking having a base class like Bunch
> > > >http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308and on
> > > > top of that keeping a list of the keys and pop/push to the list when
> > > > adding/deleting items. I don't like this idea because I'll have to
> > > > keep each key twice. (in the list and in __dict__, is this the only
> > > > way of doing it?
> >
> > > What is your objective? From the description of this recipe I cann't get
> > > your use case.
> >
> > I got an xml object which feed me in data. in a simple format
> > <item>
> > <propety1>foo</propety1>
> > <value1>bar</value1>
> > <propety2>baz</propety2>
> > <value2>bal</value2>
> > </item>
> >
> > I want to turn this into a datastructure I can manipulate in my
> > program for example.
> >
> > >>> print myItem.property1
> > >>> if myItem.property1[value1] > 0:
> >
> >        print 'ok'
> >
> > >>> print myItem
> >
> > {'property1':'value1','property2,'value2'}
> >
> >
> >
> >
> >
> > > --
> > > Jarek Zgoda
> > > Skype: jzgoda | GTalk: zg... at jabber.aster.pl | voice: +48228430101
>
> >
> > > "We read Knuth so you don't have to." (Tim Peters)
> > > --
> > >http://mail.python.org/mailman/listinfo/python-list
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



More information about the Python-list mailing list