Re: quick beginners List comprehension question

2009-01-21 Thread Srinivasa NL
You can try this

import random

class foo:
  def __init__(self):
self.bar = random.randint(1,100)


def getbar(ls,i):
  ls.append(foo())
  ls[i].bar = ls[i].bar * 3

ls = []

[getbar(ls,i) for i in range(10)]





On Thu, Jan 22, 2009 at 4:45 AM, Diez B. Roggisch wrote:

> MRAB schrieb:
>
>> Diez B. Roggisch wrote:
>>
>>> Dr Mephesto wrote:
>>>
>>>  Hi,
 Im new to python, and OOP, and am trying to get a handle on list
 comprehension.

 Say I have a class Foo with a property called bar:

 class Foo:
def __init__(self):
self.bar = random.randint(1,100)

 and then I make a list of these objects:

 Newlist = []
 for x in range(10):
Newlist.append(Foo())

 Now, suppose I wanted to triple the value of 'bar', I could always do:

 for x in range(10):
 Newlist[x].bar = Newlist[x].bar * 3

 but can I do this using list comprehension?  Thanks in Advance!

>>>
>>> No, as such, because list-comprehensions require you to have an
>>> *expression*
>>> in front of the iteration:
>>>
>>> resultlist = [ for  in ]
>>>
>>> Now what you of course can do is this:
>>>
>>> def multiply(item):
>>>item.bar = item.bar * 3
>>>
>>> [multiply(i) for i in items]
>>>
>>> However, doing this will make python produce a list of None-references -
>>> which is a waste. It's up to you if you care about that, but generally it
>>> is frowned upon because of that, and the fact that the conciseness of the
>>> list-comp here isn't really helping with the readability.
>>>
>>>  If you had:
>>
>> def multiply(item):
>>item.bar = item.bar * 3
>>return item
>>
>> then:
>>
>> [multiply(i) for i in items]
>>
>> would return items. Still a bad idea, though, because you're using a list
>> comprehension for its side-effect.
>>
>
> And redundant, which was the reason I ommited it.
>
>
> Diez
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list


Re: quick beginners List comprehension question

2009-01-21 Thread Diez B. Roggisch

MRAB schrieb:

Diez B. Roggisch wrote:

Dr Mephesto wrote:


Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension?  Thanks in Advance!


No, as such, because list-comprehensions require you to have an 
*expression*

in front of the iteration:

resultlist = [ for  in ]

Now what you of course can do is this:

def multiply(item):
item.bar = item.bar * 3

[multiply(i) for i in items]

However, doing this will make python produce a list of None-references -
which is a waste. It's up to you if you care about that, but generally it
is frowned upon because of that, and the fact that the conciseness of the
list-comp here isn't really helping with the readability.


If you had:

def multiply(item):
item.bar = item.bar * 3
return item

then:

[multiply(i) for i in items]

would return items. Still a bad idea, though, because you're using a 
list comprehension for its side-effect.


And redundant, which was the reason I ommited it.

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


Re: quick beginners List comprehension question

2009-01-21 Thread Terry Reedy

Dr Mephesto wrote:

Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())


Constructing this list is the appropriate place for a comprehension.
Newlist = [Foo() for _ in range(10)]


Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3


Use MRAB's replacement for this.


but can I do this using list comprehension?


Don't, for reasons given by others.

tjr

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


Re: quick beginners List comprehension question

2009-01-21 Thread Philip Semanchuk


On Jan 21, 2009, at 11:52 AM, Lou Pecora wrote:


In article ,
Philip Semanchuk  wrote:



Other answers have been good; to them I'll add the comment that list
comprehensions are for *constructing* lists, not manipulating the
elements thereof.

HTH
Philip



Well this seems to work just fine.  What am I missing:

  A=[1,2,3]
  print A
  A=[2*a for a in A]
  print A


You haven't manipulated the list A, you've simply overwritten it with  
a new list. 
--

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


Re: quick beginners List comprehension question

2009-01-21 Thread Steve Holden
Lou Pecora wrote:
> In article ,
>  Philip Semanchuk  wrote:
> 
>> Other answers have been good; to them I'll add the comment that list  
>> comprehensions are for *constructing* lists, not manipulating the  
>> elements thereof.
>>
>> HTH
>> Philip
> 
> 
> Well this seems to work just fine.  What am I missing:
> 
>A=[1,2,3]
>print A
>A=[2*a for a in A]
>print A
> 
The fact that the lists to be multiplied are attributes of a list of
objects, and therefore aren't themselves a list. Look more closely at
the original poster's question.

regards
 Steve
-- 
Steve Holden+1 571 484 6266   +1 800 494 3119
Holden Web LLC  http://www.holdenweb.com/

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


Re: quick beginners List comprehension question

2009-01-21 Thread Lou Pecora
In article ,
 Philip Semanchuk  wrote:

> 
> Other answers have been good; to them I'll add the comment that list  
> comprehensions are for *constructing* lists, not manipulating the  
> elements thereof.
> 
> HTH
> Philip


Well this seems to work just fine.  What am I missing:

   A=[1,2,3]
   print A
   A=[2*a for a in A]
   print A

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


Re: quick beginners List comprehension question

2009-01-21 Thread MRAB

Diez B. Roggisch wrote:

Dr Mephesto wrote:


Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension?  Thanks in Advance!


No, as such, because list-comprehensions require you to have an *expression*
in front of the iteration:

resultlist = [ for  in ]

Now what you of course can do is this:

def multiply(item):
item.bar = item.bar * 3

[multiply(i) for i in items]

However, doing this will make python produce a list of None-references -
which is a waste. It's up to you if you care about that, but generally it
is frowned upon because of that, and the fact that the conciseness of the
list-comp here isn't really helping with the readability.


If you had:

def multiply(item):
item.bar = item.bar * 3
return item

then:

[multiply(i) for i in items]

would return items. Still a bad idea, though, because you're using a 
list comprehension for its side-effect.

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


Re: quick beginners List comprehension question

2009-01-21 Thread Philip Semanchuk


On Jan 21, 2009, at 10:52 AM, Dr Mephesto wrote:


Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
   def __init__(self):
   self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
   Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension?  Thanks in Advance!


Other answers have been good; to them I'll add the comment that list  
comprehensions are for *constructing* lists, not manipulating the  
elements thereof.


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


Re: quick beginners List comprehension question

2009-01-21 Thread MRAB

Dr Mephesto wrote:

Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension?  Thanks in Advance!


You could reduce that to:

for x in Newlist:
x.bar *= 3

but I don't think you could do it with list comprehension.
--
http://mail.python.org/mailman/listinfo/python-list


Re: quick beginners List comprehension question

2009-01-21 Thread Diez B. Roggisch
Dr Mephesto wrote:

> Hi,
> Im new to python, and OOP, and am trying to get a handle on list
> comprehension.
> 
> Say I have a class Foo with a property called bar:
> 
> class Foo:
> def __init__(self):
> self.bar = random.randint(1,100)
> 
> and then I make a list of these objects:
> 
> Newlist = []
> for x in range(10):
> Newlist.append(Foo())
> 
> Now, suppose I wanted to triple the value of 'bar', I could always do:
> 
> for x in range(10):
> Newlist[x].bar = Newlist[x].bar * 3
> 
> but can I do this using list comprehension?  Thanks in Advance!

No, as such, because list-comprehensions require you to have an *expression*
in front of the iteration:

resultlist = [ for  in ]

Now what you of course can do is this:

def multiply(item):
item.bar = item.bar * 3

[multiply(i) for i in items]

However, doing this will make python produce a list of None-references -
which is a waste. It's up to you if you care about that, but generally it
is frowned upon because of that, and the fact that the conciseness of the
list-comp here isn't really helping with the readability.

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


quick beginners List comprehension question

2009-01-21 Thread Dr Mephesto
Hi,
Im new to python, and OOP, and am trying to get a handle on list
comprehension.

Say I have a class Foo with a property called bar:

class Foo:
def __init__(self):
self.bar = random.randint(1,100)

and then I make a list of these objects:

Newlist = []
for x in range(10):
Newlist.append(Foo())

Now, suppose I wanted to triple the value of 'bar', I could always do:

for x in range(10):
Newlist[x].bar = Newlist[x].bar * 3

but can I do this using list comprehension?  Thanks in Advance!



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