Re: quick beginners List comprehension question
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 = [expr for variable(s) in iterable] 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
Re: quick beginners List comprehension question
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
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
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 = [expr for variable(s) in iterable] 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
In article mailman.7691.1232554737.3487.python-l...@python.org, Philip Semanchuk phi...@semanchuk.com 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
Lou Pecora wrote: In article mailman.7691.1232554737.3487.python-l...@python.org, Philip Semanchuk phi...@semanchuk.com 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
On Jan 21, 2009, at 11:52 AM, Lou Pecora wrote: In article mailman.7691.1232554737.3487.python-l...@python.org, Philip Semanchuk phi...@semanchuk.com 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
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
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 = [expr for variable(s) in iterable] 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
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 de...@nospam.web.dewrote: 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 = [expr for variable(s) in iterable] 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