Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27055
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!usenet-fr.net!nerim.net!novso.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'modify': 0.05; 'raises': 0.07; '[0,': 0.09; '[];': 0.09; 'alain': 0.09; 'assigning': 0.09; 'dict': 0.09; 'happens.': 0.09; 'indeed,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'terry': 0.09; 'def': 0.10; 'result.': 0.15; '#this': 0.16; '(modifying': 0.16; '11:59': 0.16; 'duplicates': 0.16; 'iterating': 0.16; 'len': 0.16; 'message-id:@dough.gmane.org': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'reedy': 0.16; 'wrote:': 0.17; 'jan': 0.18; 'obviously': 0.18; '>>>': 0.18; 'skip:p 30': 0.20; 'posted': 0.22; 'work.': 0.23; 'seems': 0.23; 'header:In-Reply- To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'header:X -Complaints-To:1': 0.28; 'run': 0.28; 'agreed.': 0.29; 'faster,': 0.29; 'routine': 0.29; 'str': 0.29; 'writes:': 0.29; 'handled': 0.29; 'on,': 0.30; 'at:': 0.31; 'code': 0.31; 'gets': 0.32; 'url:python': 0.32; 'print': 0.32; 'asked': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; "can't": 0.34; 'list': 0.35; 'doing': 0.35; 'something': 0.35; 'there': 0.35; 'received:org': 0.36; 'except': 0.36; 'url:org': 0.36; 'does': 0.37; 'why': 0.37; 'item': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'to:addr:python.org': 0.39; 'notice': 0.39; 'header:Received:5': 0.40; 'your': 0.60; "you'll": 0.62; 'back': 0.62; 'email addr:gmail.com': 0.63; 'received:fios.verizon.net': 0.84; 'removals': 0.84; 'url:reference': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: Strange behavior |
| Date | Tue, 14 Aug 2012 15:05:43 -0400 |
| References | <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com> <87lihhpiq9.fsf@dpt-info.u-strasbg.fr> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Gmane-NNTP-Posting-Host | pool-173-75-251-66.phlapa.fios.verizon.net |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:14.0) Gecko/20120713 Thunderbird/14.0 |
| In-Reply-To | <87lihhpiq9.fsf@dpt-info.u-strasbg.fr> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.3284.1344971165.4697.python-list@python.org> (permalink) |
| Lines | 80 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1344971165 news.xs4all.nl 6953 [2001:888:2000:d::a6]:54393 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:27055 |
Show key headers only | View raw
On 8/14/2012 11:59 AM, Alain Ketterlin wrote:
> light1quark@gmail.com writes:
>
>> However if you run the code you will notice only one of the strings
>> beginning with 'x' is removed from the startingList.
>
>>
>> def testFunc(startingList):
>> xOnlyList = [];
>> for str in startingList:
>> if (str[0] == 'x'):
>> print str;
>> xOnlyList.append(str)
>> startingList.remove(str) #this seems to be the problem
>> print xOnlyList;
>> print startingList
>> testFunc(['xasd', 'xjkl', 'sefwr', 'dfsews'])
>>
>> #Thanks for your help!
>
> Try with ['xasd', 'sefwr', 'xjkl', 'dfsews'] and you'll understand what
> happens. Also, have a look at:
>
> http://docs.python.org/reference/compound_stmts.html#the-for-statement
>
> You can't modify the list you're iterating on,
Except he obviously did ;-).
(Modifying set or dict raises SomeError.)
Indeed, people routine *replace* items while iterating.
def squarelist(lis):
for i, n in enumerate(lis):
lis[i] = n*n
return lis
print(squarelist([0,1,2,3,4,5]))
# [0, 1, 4, 9, 16, 25]
Removals can be handled by iterating in reverse. This works even with
duplicates because if the item removed is not the one tested, the one
tested gets retested.
def removeodd(lis):
for n in reversed(lis):
if n % 2:
lis.remove(n)
print(n, lis)
ll = [0,1, 5, 5, 4, 5]
removeodd(ll)
>>>
5 [0, 1, 5, 4, 5]
5 [0, 1, 4, 5]
5 [0, 1, 4]
4 [0, 1, 4]
1 [0, 4]
0 [0, 4]
> better use another list to collect the result.
If there are very many removals, a new list will be faster, even if one
needs to copy the new list back into the original, as k removals from
len n list is O(k*n) versus O(n) for new list and copy.
> P/S: str is a builtin, you'd better avoid assigning to it.
Agreed. People have actually posted code doing something like
...
list = [1,2,3]
...
z = list(x)
...
and wondered and asked why it does not work.
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Strange behavior light1quark@gmail.com - 2012-08-14 08:38 -0700
Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-14 17:59 +0200
Re: Strange behavior Terry Reedy <tjreedy@udel.edu> - 2012-08-14 15:05 -0400
Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-14 21:40 +0200
Re: Strange behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-15 00:19 +0000
Re: Strange behavior Virgil Stokes <vs@it.uu.se> - 2012-08-16 13:18 +0200
Re: Strange behavior Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-08-16 17:40 +0000
Re: Strange behavior Peter Otten <__peter__@web.de> - 2012-08-16 15:02 +0200
Re: Strange behavior light1quark@gmail.com - 2012-08-14 12:20 -0700
Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-15 11:57 +0200
Re: Strange behavior Chris Angelico <rosuav@gmail.com> - 2012-08-15 07:55 +1000
Re: Strange behavior Alain Ketterlin <alain@dpt-info.u-strasbg.fr> - 2012-08-15 11:50 +0200
csiph-web