Re: string issue

2005-02-08 Thread Reinhold Birkenfeld
Alex Martelli wrote: > Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote: > >> Alex Martelli wrote: >> >> > So, *WHAT ON EARTH* could possibly >> > make this weird 'x[:]' form preferable to 'x*1'?! It's MUCH more >> > obvious that the second one returns an independent, separate object >> > initially

Re: string issue

2005-02-07 Thread Alex Martelli
Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote: > Alex Martelli wrote: > > > So, *WHAT ON EARTH* could possibly > > make this weird 'x[:]' form preferable to 'x*1'?! It's MUCH more > > obvious that the second one returns an independent, separate object > > initially equal to x > > .>> x = 2 > .>

Re: string issue

2005-02-07 Thread Reinhold Birkenfeld
Alex Martelli wrote: > So, *WHAT ON EARTH* could possibly > make this weird 'x[:]' form preferable to 'x*1'?! It's MUCH more > obvious that the second one returns an independent, separate object > initially equal to x .>> x = 2 .>> y = x*1 .>> x is y True .>> just-kidding-ly yours, Reinhold --

Re: string issue

2005-02-07 Thread Alex Martelli
Reinhold Birkenfeld <[EMAIL PROTECTED]> wrote: ... > > Using ips[:] to make a copy on the fly is very idiomatic, but I've never > > liked it, personally. I see no reason to prefer it to the even shorter > > and arguably less obscure ips*1, for example. > > "Less obscure"? Either you know the `

Re: string issue

2005-02-07 Thread Reinhold Birkenfeld
Alex Martelli wrote: > Bill Mill <[EMAIL PROTECTED]> wrote: >... >> > > You are modifying the list as you iterate over it. Instead, iterate over >> > > a copy by using: >> > > >> > > for ip in ips[:]: >... >> Once you know it, it's neat, and I use it sometimes. However, it's a >> little too

Re: string issue

2005-02-06 Thread Alex Martelli
Bill Mill <[EMAIL PROTECTED]> wrote: ... > > > You are modifying the list as you iterate over it. Instead, iterate over > > > a copy by using: > > > > > > for ip in ips[:]: ... > Once you know it, it's neat, and I use it sometimes. However, it's a > little too "magical" for my tastes; I'd rat

Re: string issue

2005-02-06 Thread Francis Girard
Yes, I also this that comprehension is the clearer way to handle this. You might also consider the good old "filter" function : ips = filter(lambda ip: '255' not in ip, ips) Francis Girard Le vendredi 4 Février 2005 20:38, rbt a écrit : > Thanks guys... list comprehension it is! > > Bill Mill w

Re: string issue

2005-02-04 Thread Nick Coghlan
rbt wrote: Alan McIntyre wrote: I think it's because you're modifying the list as you're iterating over it. One last clarification on this. It's OK to modify the elements of a list, but not the list itself while iterating over it... is that the correct way to think about this? Correct - the i

Re: string issue

2005-02-04 Thread Steven Bethard
rbt wrote: John J. Lee wrote: Steve Holden <[EMAIL PROTECTED]> writes: [...] You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... Just to help popularise the alternative idiom, which IMO is significantly less cryptic (sane constructors of

Re: string issue

2005-02-04 Thread Steve Holden
Terry Reedy wrote: "Steve Holden" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] [ ... ] This seems much more sensible to me than building a new list with everything (a copy), including things you don't want, and then deleting the things you don't want (an O(n**2) operation). Whic

Re: string issue

2005-02-04 Thread Terry Reedy
"Steve Holden" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > You are modifying the list as you iterate over it. Instead, iterate over > > a copy by using: > for ip in ips[:]: Or if you are only deleting items, you can iterate backwards. You can also build a new list with only

Re: string issue

2005-02-04 Thread rbt
Bill Mill wrote: On Fri, 04 Feb 2005 15:25:04 -0500, rbt <[EMAIL PROTECTED]> wrote: John J. Lee wrote: Steve Holden <[EMAIL PROTECTED]> writes: [...] You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... Just to help popularise the altern

Re: string issue

2005-02-04 Thread Bill Mill
On Fri, 04 Feb 2005 15:25:04 -0500, rbt <[EMAIL PROTECTED]> wrote: > John J. Lee wrote: > > Steve Holden <[EMAIL PROTECTED]> writes: > > [...] > > > >>You are modifying the list as you iterate over it. Instead, iterate > >>over a copy by using: > >> > >>for ip in ips[:]: > >> ... > > > > > > Just

Re: string issue

2005-02-04 Thread Jeremy Bowers
On Fri, 04 Feb 2005 14:49:43 -0500, rbt wrote: > Alan McIntyre wrote: >> I think it's because you're modifying the list as you're iterating over > > In this case then, shouldn't my 'except Exception' raise an error or > warning like: > > "Hey, stupid, you can't iterate and object and change it

Re: string issue

2005-02-04 Thread rbt
John J. Lee wrote: Steve Holden <[EMAIL PROTECTED]> writes: [...] You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... Just to help popularise the alternative idiom, which IMO is significantly less cryptic (sane constructors of mutable o

Re: string issue

2005-02-04 Thread John J. Lee
Steve Holden <[EMAIL PROTECTED]> writes: [...] > You are modifying the list as you iterate over it. Instead, iterate > over a copy by using: > > for ip in ips[:]: >... Just to help popularise the alternative idiom, which IMO is significantly less cryptic (sane constructors of mutable objects

Re: string issue

2005-02-04 Thread rbt
Alan McIntyre wrote: I think it's because you're modifying the list as you're iterating over it. One last clarification on this. It's OK to modify the elements of a list, but not the list itself while iterating over it... is that the correct way to think about this? -- http://mail.python.org/m

Re: string issue

2005-02-04 Thread Bill Mill
On Fri, 04 Feb 2005 14:43:30 -0500, rbt <[EMAIL PROTECTED]> wrote: > Steve Holden wrote: > > rbt wrote: > > > >> Either I'm crazy and I'm missing the obvious here or there is > >> something wrong with this code. Element 5 of this list says it doesn't > >> contain the string 255, when that's *ALL* i

Re: string issue

2005-02-04 Thread rbt
Alan McIntyre wrote: I think it's because you're modifying the list as you're iterating over In this case then, shouldn't my 'except Exception' raise an error or warning like: "Hey, stupid, you can't iterate and object and change it at the same time!" Or, perhaps something similar? -- http://mai

Re: string issue

2005-02-04 Thread Steven Bethard
Steve Holden wrote: You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... Also worth noting, you can write this like: for ip in list(ips): ... if you're afraid of smiley-faces [:] in your code. ;) Steve -- http://mail.python.

Re: string issue

2005-02-04 Thread Fredrik Lundh
"rbt" <[EMAIL PROTECTED]> wrote: >> You are modifying the list as you iterate over it. Instead, iterate over a >> copy by using: >> >> for ip in ips[:]: >> ... >> >> regards >> Steve > > Very neat. That's a trick that everyone should know about. I suppose that's why it's included in the Pytho

Re: string issue

2005-02-04 Thread rbt
Steve Holden wrote: rbt wrote: Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL* it contains... why would it think that??? import time ips = ['255.255.255.255', '128.173.

Re: string issue

2005-02-04 Thread Alan McIntyre
Wow, that's cool; I'd never seen that before. :) Thanks, Steve.. Steve Holden wrote: You are modifying the list as you iterate over it. Instead, iterate over a copy by using: for ip in ips[:]: ... regards Steve -- http://mail.python.org/mailman/listinfo/python-list

Re: string issue

2005-02-04 Thread rbt
Thanks guys... list comprehension it is! Bill Mill wrote: On Fri, 04 Feb 2005 14:23:36 -0500, rbt <[EMAIL PROTECTED]> wrote: Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL*

Re: string issue

2005-02-04 Thread Steve Holden
rbt wrote: Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL* it contains... why would it think that??? import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.

Re: string issue

2005-02-04 Thread Steven Bethard
rbt wrote: Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL* it contains... why would it think that??? import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.

Re: string issue

2005-02-04 Thread Alan McIntyre
I think it's because you're modifying the list as you're iterating over it. Try this: import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', '127.0.0.1', '255.0.0.0', '255', '128.173.255.34'] ips_new = [] for ip in ips: if '255' not in ip: ips_new.append(ip) print

Re: string issue

2005-02-04 Thread Bill Mill
On Fri, 04 Feb 2005 14:23:36 -0500, rbt <[EMAIL PROTECTED]> wrote: > Either I'm crazy and I'm missing the obvious here or there is something > wrong with this code. Element 5 of this list says it doesn't contain the > string 255, when that's *ALL* it contains... why would it think that??? > > impo

string issue

2005-02-04 Thread rbt
Either I'm crazy and I'm missing the obvious here or there is something wrong with this code. Element 5 of this list says it doesn't contain the string 255, when that's *ALL* it contains... why would it think that??? import time ips = ['255.255.255.255', '128.173.120.79', '198.82.247.98', '127.