Hi,

Yes, sorry I haven't posted to the list in a while. I should have been more specific. I'm writing a simple contact database, using metakit as the backend. Thank you for pointing out that what I was trying to do was easier than I believed.

Here's some code.

db = metakit.storage('c:/addy.mk',1)
vw = db.getas('contacts[first:S,last:S,phone:S,email:S,notes:S]')

desc = ('first', 'last', 'phone', 'email', 'notes')

def listItems():
    l= []
    d = {}
    for r in range(len(vw)):
        d = {'first':vw[r].first, 'last':vw[r].last, 'phone':vw[r].phone, 'email':vw[r].email, 'notes':vw[r].notes}
        l.append(d)
    return l

At the moment I would like to generate the listItems dynamically, from the desc variable, so new databases can be added without changing the code.

I thought that if:

def listItems():
    l= []
    d = {}
    lt = len(desc)
    for r in range(len(vw)):
        for x in range(len(lt)):
            d[desc[x]] = exec("""'vw'+[r]+'.'+desc[x]""")
        l.append(d)
    return l

Whereby the vw metakit object behaves like a dictionary, and the exec statement isn't usable in the way I would wish for.

Luis N.

On 6/28/05, Brian van den Broek <[EMAIL PROTECTED] > wrote:
Luis N said unto the world upon 28/06/2005 15:25:
> Hi,
>
>
>>>>l
>
> [{'last': 'Bar', 'first': 'Foo'}, {'last': 'Bar', 'first': 'Foo'}, {'last':
> 'Bar', 'first': 'Foo'}, {'last': 'Bar', 'first': 'Foo'}]
>
>
> This is how I imagine it:
>
> for i in l:
> for j in l[i]:
> for k in l[i][j]:
> print k.get('first')
> print k.get('last')
>
> Is there a short and sweet way of doing this (that actually works).
>
> Luis.

Hi Luis,

I'm not certain I see what you are wanting from your description.
(You've got more nesting in your loops than in l.) But does this do
what is wanted?

>>> a_list = [ {'last': 'Bar', 'first': 'Foo'},
                {'last': 'Bar', 'first': 'Foo'},
                {'last': 'Bar', 'first': 'Foo'},
                {'last': 'Bar', 'first': 'Foo'} ]
>>> for a_dict in a_list:
         print a_dict['first']
         print a_dict['last']


Foo
Bar
Foo
Bar
Foo
Bar
Foo
Bar
>>>


If it does, why are you doing this? Is it to figure out how to
manipulate data structures with Python? If so, good. If you are trying
to do real work this way, there is surely a better way. Maybe if you
said what you are trying to accomplish, someone could help you find a
good way to do it.

Best,

Brian vdB



_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to