How to del item of a list in loop?

2005-01-15 Thread skull

Hi everybody, it is my first post in this newsgroup.
I am a newbie for python though I have several years development experience in 
c++.
recently, I was stumped when I tried to del item of a list when iteration.

here is the wrong way I did:

lst = [1, 2, 3]
for i in lst:
print i
if i == 2: 
   lst.remove(i)

the result is:

1
2
>>>

as you would see, '3' is missing. this problem is caused by 'lst.remove(i)'.
apparently, 'marked-and-sweep' is a solution to deal with this issue.
but I think there SHOULD BE more 'wise' trick. I want to get your help.

Thanks in advance.

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


Re: How to del item of a list in loop?

2005-01-15 Thread skull
skull <[EMAIL PROTECTED]> writes:

Thank you for your replys.
lst[:] is did a solution, it makes a copy of list specially for iteration and 
removes items from the original one.

but I still have an other thing to worry about coming with this way: does 
performance sucks when the list is big enough?
It makes a copy operation!

here is a faster and 'ugly' solution:

lst = [1, 2, 3]
i = 0
while i < len(lst):
if lst[i] == 2:
lst.remove(i)
else:
i += 1

> Hi everybody, it is my first post in this newsgroup.
> I am a newbie for python though I have several years development experience 
> in c++.
> recently, I was stumped when I tried to del item of a list when iteration.
>
> here is the wrong way I did:
>
> lst = [1, 2, 3]
> for i in lst:
> print i
> if i == 2: 
>lst.remove(i)
>
> the result is:
>
> 1
> 2
>>>>
>
> as you would see, '3' is missing. this problem is caused by 'lst.remove(i)'.
> apparently, 'marked-and-sweep' is a solution to deal with this issue.
> but I think there SHOULD BE more 'wise' trick. I want to get your help.
>
> Thanks in advance.
>
> - skull
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: How to del item of a list in loop?

2005-01-15 Thread skull
Reinhold Birkenfeld <[EMAIL PROTECTED]> writes:

>> Quick solution:
>> 
>> for i in lst[:]
>> 
>> iterates over a copy.
>
> Addition: In Py2.4, I can't find a problem with
>
> for i in reversed(lst)
>
> Any objections?
>
> Reinhold

I just downloaded py2.4, and made a test using reversed.
it sure be no problem, I thought maybe the reversed holded a copy of list,
and eventually iterated through the copy.
but the truth is not as I thought so:

import sys

class Test:
pass

lst = [Test(),Test(),Test()]

E1: for i in lst[:]:
E2: for i in reversed(lst):
print sys.getrefcount(i)

###
E1 outputs:
4
4
4

E2 outputs:
3
3
3

It looks that the reversed does not make a copy of list in contrast with lst[:].
so should we regard: reversed is faster than lst[:]?
I do not have any idea about why it is.

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


Re: Is the standard output thread-safe?

2006-03-12 Thread Skull
Fernando Rodríguez <[EMAIL PROTECTED]> writes:

> Hi,
>
> Is the standard output thread-safe? Can I use print from several
> threads without having to use a mutex?
>
> Thanks
>
>

Check this: 
http://me.in-berlin.de/doc/python/faq/library.html#what-kinds-of-global-value-mutation-are-thread-safe

from my experience, it seems 'print' is not thread-safe. especially
when it is called with formated string, such as:

print "id:%d", my_id

I think it is not an "atomic" operation.

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