newb: Scope Question

attn.steven.kuo at gmail.com attn.steven.kuo at gmail.com
Fri Jun 22 19:25:19 EDT 2007


On Jun 22, 3:53 pm, johnny <rampet... at gmail.com> wrote:
> Scope of ids:
> When I print "ids", it's always empty string '', as I have intialized
> before.  That's not what I want. I want the ids to have
> str(r['id']).join(',')
>
> if res:
>                 ids = ''
>                 for r in res['key']:
>                     ids = str(r['id']).join(',')
>
>                 print("ids: %s" %(ids))




"""
1.  You haven't posted enough code to allow someone else to reproduce
the problem.

2.  Are you sure that res['key'] isn't an empty sequence?

3.  The 'join' operation looks funny.  The syntax is
    <separator>.join(<sequence>).  Since ',' is a sequence of length
    one, you'll get, if anything, a comma (',') as the value of ids.
"""

res = { 'key': [dict(id=value) for value in range(10)] }

if res:
    ids = ''
    for r in res['key']:
        ids = str(r['id']).join(',')
    print ("ids : %s" % (ids))


# Wild guess -- perhaps you really wanted:


if res:
    ids = ','.join(str(r['id']) for r in res['key'])
    print "ids : %s" % ids


--
Hope this helps,
Steven




More information about the Python-list mailing list