Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

newbie question - python lists

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


san82moon at gmail

Nov 6, 2009, 6:15 AM

Post #1 of 15 (450 views)
Permalink
newbie question - python lists

hi,

rfids= ['01','02']
i = 01
row = {}
items = []
for rfid in rfids:

brains = ['1','2']

if brains:
for brain in brains:
# this loop must run only once for each value of i
row['itemnos'] = 'item_0'+str(i)+'s'
print 'hi'
items.append(row)
print items
break
i=i+1

the above code produces output a:
[{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
but i want it to be,
[{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]

can anyone point wer am erroring.
Thanks in advance.
--
http://mail.python.org/mailman/listinfo/python-list


jim.hefferon at gmail

Nov 6, 2009, 6:48 AM

Post #2 of 15 (429 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
> can anyone point wer am erroring.

I'm not sure what you are trying to do, but it is odd, isn't it, that
you never refer to brain in the "for brain in brains:" loop? I think
you are mixing i and brain somehow.

Jim
--
http://mail.python.org/mailman/listinfo/python-list


brian.curtin at gmail

Nov 6, 2009, 6:56 AM

Post #3 of 15 (430 views)
Permalink
Re: newbie question - python lists [In reply to]

On Fri, Nov 6, 2009 at 08:15, lee <san82moon [at] gmail> wrote:

> hi,
>
> rfids= ['01','02']
> i = 01
> row = {}
> items = []
> for rfid in rfids:
>
> brains = ['1','2']
>
> if brains:
> for brain in brains:
> # this loop must run only once for each value of i
> row['itemnos'] = 'item_0'+str(i)+'s'
> print 'hi'
> items.append(row)
> print items
> break
> i=i+1
>
> the above code produces output a:
> [{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
> but i want it to be,
> [{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]
>
> can anyone point wer am erroring.
> Thanks in advance.
>

Your call to items.append(row) passes row by reference. You added a row
dictionary with key "itemnos" set to value "item_01s" the first time through
the loop. The second time, you added a row dictionary with key "itemnos" set
to value "item_02s". This modified the underlying `row` dictionary, so it
also modified what you thought you put in the list in the first time through
the loop.


joncle at googlemail

Nov 6, 2009, 7:17 AM

Post #4 of 15 (429 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 2:15 pm, lee <san82m...@gmail.com> wrote:
> hi,
>
> rfids= ['01','02']
> i = 01
> row = {}
> items = []
> for rfid in rfids:
>
>     brains = ['1','2']
>
>     if brains:
>         for brain in brains:
>             # this loop must run only once for each value of i
>             row['itemnos'] = 'item_0'+str(i)+'s'
>             print 'hi'
>             items.append(row)
>             print items
>             break
>     i=i+1
>
> the above code produces output a:
> [{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
> but i want it to be,
> [{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]
>
> can anyone point wer am erroring.
> Thanks in advance.

You've made a good effort (and discovered at least one major common
gotcha).

There's lots in your code that needs highlighting. However, probably a
good starting point would to be describe in plain english, what you're
trying to achieve; Are you really sure you want a list of single item
dict's?

Jon.
--
http://mail.python.org/mailman/listinfo/python-list


rpjday at crashcourse

Nov 6, 2009, 7:31 AM

Post #5 of 15 (429 views)
Permalink
Re: newbie question - python lists [In reply to]

On Fri, 6 Nov 2009, lee wrote:

> hi,
>
> rfids= ['01','02']
> i = 01

for what it's worth, that statement above isn't even legal python3.

> row = {}
> items = []
> for rfid in rfids:
>
> brains = ['1','2']
>
> if brains:
> for brain in brains:
> # this loop must run only once for each value of i
> row['itemnos'] = 'item_0'+str(i)+'s'
> print 'hi'
> items.append(row)
> print items
> break
> i=i+1

rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
--
http://mail.python.org/mailman/listinfo/python-list


san82moon at gmail

Nov 6, 2009, 8:47 AM

Post #6 of 15 (429 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
> On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>
> > can anyone point wer am erroring.
>
> I'm not sure what you are trying to do, but it is odd, isn't it, that
> you never refer to brain in the "for brain in brains:" loop?  I think
> you are mixing i and brain somehow.
>
> Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.
--
http://mail.python.org/mailman/listinfo/python-list


san82moon at gmail

Nov 6, 2009, 8:49 AM

Post #7 of 15 (422 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
> On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>
> > can anyone point wer am erroring.
>
> I'm not sure what you are trying to do, but it is odd, isn't it, that
> you never refer to brain in the "for brain in brains:" loop?  I think
> you are mixing i and brain somehow.
>
> Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.
--
http://mail.python.org/mailman/listinfo/python-list


san82moon at gmail

Nov 6, 2009, 8:56 AM

Post #8 of 15 (424 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
> On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>
> > can anyone point wer am erroring.
>
> I'm not sure what you are trying to do, but it is odd, isn't it, that
> you never refer to brain in the "for brain in brains:" loop?  I think
> you are mixing i and brain somehow.
>
> Jim

ok let me make it clear,

brains = ['1','2']
for brain in brains:
row['item'] = brain
items.append(row)
print items

This produces
[{'item': '1'}]
[{'item': '2'}, {'item': '2'}]
but i want
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]

if i do items.append(brain), it gives,
['1', '2']
but i want dictionary inside list.
@Jon - Yes i want single item dict's
@Robert - i use python 2.4 .

Thanks
Lee.
--
http://mail.python.org/mailman/listinfo/python-list


emily at nospam

Nov 6, 2009, 9:00 AM

Post #9 of 15 (422 views)
Permalink
Re: newbie question - python lists [In reply to]

"lee" <san82moon [at] gmail> wrote in message
news:8d1009a4-54c8-4bb3-9862-8b22e6b42a3b [at] y10g2000prg
> hi,
>
> rfids= ['01','02']
> i = 01
> row = {}
> items = []
> for rfid in rfids:
>
> brains = ['1','2']
>
> if brains:
> for brain in brains:
> # this loop must run only once for each value of i
> row['itemnos'] = 'item_0'+str(i)+'s'
> print 'hi'
> items.append(row)
> print items
> break
> i=i+1
>
> the above code produces output a:
> [{'itemnos': 'item_02s'}, {'itemnos': 'item_02s'}]
> but i want it to be,
> [{'itemnos': 'item_01s'}, {'itemnos': 'item_02s'}]
>
> can anyone point wer am erroring.
> Thanks in advance.

Hi,

As some others have pointed out, it is not totally clear from your code what
you are trying to do, but I will forgive you for that because you are
clearly not used to python!

I think what you are trying to do is:

items = []
rfids= ['01','02']
for rfid in rfids:
items.append({'itemnos': 'item_%ss' % rfid})
print items

This can also be done using a list comprehension which is one of the (many)
most useful features of python (imo):
rfids= ['01','02']
print [{'itemnos': 'item_%ss' % rfid} for rfid in rfids]

Or on one line (although it is always to be clearer rather trying to fit
stuff into one line - it is not perl!)

print [{'itemnos': 'item_%ss' % rfid} for rfid in ['01', '02']]

Hope this helps.

Emily


--
http://mail.python.org/mailman/listinfo/python-list


rpjday at crashcourse

Nov 6, 2009, 9:05 AM

Post #10 of 15 (422 views)
Permalink
Re: newbie question - python lists [In reply to]

On Fri, 6 Nov 2009, lee wrote:

> brains = ['1','2']
> for brain in brains:
> row['item'] = brain
> items.append(row)
> print items
>
> This produces
> [{'item': '1'}]
> [{'item': '2'}, {'item': '2'}]
> but i want
> [{'item': '1'}]
> [{'item': '1'}, {'item': '2'}]
>
> if i do items.append(brain), it gives,
> ['1', '2']
> but i want dictionary inside list.
> @Jon - Yes i want single item dict's
> @Robert - i use python 2.4 .

this seems to work:

items = []
brains = ['1','2','3','4']
for brain in brains:
items.append({'item':brain})
print(items)

>>> import t1
[{'item': '1'}]
[{'item': '1'}, {'item': '2'}]
[{'item': '1'}, {'item': '2'}, {'item': '3'}]
[{'item': '1'}, {'item': '2'}, {'item': '3'}, {'item': '4'}]


rday
--

========================================================================
Robert P. J. Day Waterloo, Ontario, CANADA

Linux Consulting, Training and Kernel Pedantry.

Web page: http://crashcourse.ca
Twitter: http://twitter.com/rpjday
========================================================================
--
http://mail.python.org/mailman/listinfo/python-list


benjamin.kaplan at case

Nov 6, 2009, 9:07 AM

Post #11 of 15 (423 views)
Permalink
Re: newbie question - python lists [In reply to]

On Fri, Nov 6, 2009 at 11:49 AM, lee <san82moon [at] gmail> wrote:
> On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
>> On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>>
>> > can anyone point wer am erroring.
>>
>> I'm not sure what you are trying to do, but it is odd, isn't it, that
>> you never refer to brain in the "for brain in brains:" loop?  I think
>> you are mixing i and brain somehow.
>>
>> Jim
>
> ok let me make it clear,
>
> brains = ['1','2']
> for brain in brains:
>    row['item'] = brain
>    items.append(row)
>    print items
>
> This produces
> [{'item': '1'}]
> [{'item': '2'}, {'item': '2'}]
> but i want
> [{'item': '1'}]
> [{'item': '1'}, {'item': '2'}]
>
> if i do items.append(brain), it gives,
> ['1', '2']
> but i want dictionary inside list.
> @Jon - Yes i want single item dict's
> @Robert - i use python 2.4 .
>
> Thanks
> Lee.

With Python, you're always dealing with objects, not with values. It
isn't apparent with immutable objects like strings and ints, but
here's what's actually happening. At the end, your list is [row, row].
It's the exact same dictionary. The value wasn't copied in. Try this

row = {}
row['item'] = 1

items = [row]
row['item'] = 2
print row

You get the same thing- you're changing the dict that's already in
items. In order to get two different elements, you have to use 2
different dicts.

if brains:
for brain in brains:
# this loop must run only once for each value of i
row = { 'itemnos': 'item_0'+str(i)+'s'}
print 'hi'
items.append(row)
print items
break
i=i+1


> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


rami.chowdhury at gmail

Nov 6, 2009, 9:12 AM

Post #12 of 15 (422 views)
Permalink
Re: newbie question - python lists [In reply to]

On Fri, 06 Nov 2009 08:49:52 -0800, lee <san82moon [at] gmail> wrote:

> On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
>> On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>>
>> > can anyone point wer am erroring.
>>
>> I'm not sure what you are trying to do, but it is odd, isn't it, that
>> you never refer to brain in the "for brain in brains:" loop?  I think
>> you are mixing i and brain somehow.
>>
>> Jim
>
> ok let me make it clear,
>
> brains = ['1','2']
> for brain in brains:
> row['item'] = brain
> items.append(row)
> print items
>
> This produces
> [{'item': '1'}]
> [{'item': '2'}, {'item': '2'}]
> but i want
> [{'item': '1'}]
> [{'item': '1'}, {'item': '2'}]
>

Lee,

When you do
> row['item'] = brain
you are actually modifying the existing dictionary object called 'row' --
and I get the impression this is not what you want to do.

I'd suggest creating a new dictionary on each pass, which should give you
the desired behavior.

--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


san82moon at gmail

Nov 6, 2009, 9:19 AM

Post #13 of 15 (422 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 9:47 pm, lee <san82m...@gmail.com> wrote:
> On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
>
> > On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>
> > > can anyone point wer am erroring.
>
> > I'm not sure what you are trying to do, but it is odd, isn't it, that
> > you never refer to brain in the "for brain in brains:" loop?  I think
> > you are mixing i and brain somehow.
>
> > Jim
>
> ok let me make it clear,
>
> brains = ['1','2']
> for brain in brains:
>     row['item'] = brain
>     items.append(row)
>     print items
>
> This produces
> [{'item': '1'}]
> [{'item': '2'}, {'item': '2'}]
> but i want
> [{'item': '1'}]
> [{'item': '1'}, {'item': '2'}]
>
> if i do items.append(brain), it gives,
> ['1', '2']
> but i want dictionary inside list.
> @Jon - Yes i want single item dict's
> @Robert - i use python 2.4 .
>
> Thanks
> Lee.

i got it solved ,

items = []
for brain in brains:
dict = {'itemnos': 'item_0' + brain + 's'}
items.append(dict)
print(items)

thanks all.
Lee.
--
http://mail.python.org/mailman/listinfo/python-list


rami.chowdhury at gmail

Nov 6, 2009, 9:47 AM

Post #14 of 15 (422 views)
Permalink
Re: newbie question - python lists [In reply to]

On Fri, 06 Nov 2009 09:19:00 -0800, lee <san82moon [at] gmail> wrote:

> On Nov 6, 9:47 pm, lee <san82m...@gmail.com> wrote:
>> On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
>>
>> > On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>>
>> > > can anyone point wer am erroring.
>>
>> > I'm not sure what you are trying to do, but it is odd, isn't it, that
>> > you never refer to brain in the "for brain in brains:" loop?  I think
>> > you are mixing i and brain somehow.
>>
>> > Jim
>>
>> ok let me make it clear,
>>
>> brains = ['1','2']
>> for brain in brains:
>>     row['item'] = brain
>>     items.append(row)
>>     print items
>>
>> This produces
>> [{'item': '1'}]
>> [{'item': '2'}, {'item': '2'}]
>> but i want
>> [{'item': '1'}]
>> [{'item': '1'}, {'item': '2'}]
>>
>> if i do items.append(brain), it gives,
>> ['1', '2']
>> but i want dictionary inside list.
>> @Jon - Yes i want single item dict's
>> @Robert - i use python 2.4 .
>>
>> Thanks
>> Lee.
>
> i got it solved ,
>
> items = []
> for brain in brains:
> dict = {'itemnos': 'item_0' + brain + 's'}
> items.append(dict)
> print(items)
>

Glad to see you solved it! A couple of minor considerations:
- I would suggest you use a variable name other than 'dict', as that
shadows the built-in 'dict' type
- I would suggest using the idiom 'item_0%ss' % brain rather than
'item_0' + brain + 's', in case at some point you want to change the type
of 'brain'


--
Rami Chowdhury
"Never attribute to malice that which can be attributed to stupidity" --
Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)
--
http://mail.python.org/mailman/listinfo/python-list


san82moon at gmail

Nov 6, 2009, 10:01 AM

Post #15 of 15 (414 views)
Permalink
Re: newbie question - python lists [In reply to]

On Nov 6, 10:47 pm, "Rami Chowdhury" <rami.chowdh...@gmail.com> wrote:
> On Fri, 06 Nov 2009 09:19:00 -0800, lee <san82m...@gmail.com> wrote:
> > On Nov 6, 9:47 pm, lee <san82m...@gmail.com> wrote:
> >> On Nov 6, 7:48 pm, Jim <jim.heffe...@gmail.com> wrote:
>
> >> > On Nov 6, 9:15 am, lee <san82m...@gmail.com> wrote:
>
> >> > > can anyone point wer am erroring.
>
> >> > I'm not sure what you are trying to do, but it is odd, isn't it, that
> >> > you never refer to brain in the "for brain in brains:" loop?  I think
> >> > you are mixing i and brain somehow.
>
> >> > Jim
>
> >> ok let me make it clear,
>
> >> brains = ['1','2']
> >> for brain in brains:
> >>     row['item'] = brain
> >>     items.append(row)
> >>     print items
>
> >> This produces
> >> [{'item': '1'}]
> >> [{'item': '2'}, {'item': '2'}]
> >> but i want
> >> [{'item': '1'}]
> >> [{'item': '1'}, {'item': '2'}]
>
> >> if i do items.append(brain), it gives,
> >> ['1', '2']
> >> but i want dictionary inside list.
> >> @Jon - Yes i want single item dict's
> >> @Robert - i use python 2.4 .
>
> >> Thanks
> >> Lee.
>
> > i got it solved ,
>
> > items = []
> > for brain in brains:
> >        dict = {'itemnos': 'item_0' + brain + 's'}
> >        items.append(dict)
> > print(items)
>
> Glad to see you solved it! A couple of minor considerations:
>         - I would suggest you use a variable name other than 'dict', as that  
> shadows the built-in 'dict' type
>         - I would suggest using the idiom 'item_0%ss' % brain rather than  
> 'item_0' + brain + 's', in case at some point you want to change the type  
> of 'brain'
>
> --
> Rami Chowdhury
> "Never attribute to malice that which can be attributed to stupidity" --  
> Hanlon's Razor
> 408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)

i greatly value your suggestion.
i have made changes as indicted by you.
The code looks more clean now.

Thank you,
Lee.
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.