Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #27056
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!newsfeed.eweka.nl!eweka.nl!feeder3.eweka.nl!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <vs@it.uu.se> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.005 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'removes': 0.05; 'puts': 0.07; 'python': 0.09; '[];': 0.09; 'happen?': 0.09; 'def': 0.10; 'slightly': 0.15; '#this': 0.16; '-1:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'runs': 0.22; 'statement': 0.23; 'seems': 0.23; 'second': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'noticed': 0.28; 'run': 0.28; 'finds': 0.29; 'str': 0.29; 'function': 0.30; 'code': 0.31; "skip:' 20": 0.32; 'print': 0.32; 'function.': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'another': 0.33; 'version': 0.34; 'list': 0.35; 'identified': 0.35; 'does': 0.37; 'being': 0.37; 'why': 0.37; 'subject:: ': 0.38; 'comment': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'received:192': 0.39; 'notice': 0.39; 'received:192.168': 0.40; 'your': 0.60; 'email addr:gmail.com': 0.63; 'making': 0.64; 'here': 0.65; 'migrating': 0.91 |
| X-SENDER-IP | [213.112.50.224] |
| X-LISTENER | [smtp.bredband.net] |
| X-IronPort-Anti-Spam-Filtered | true |
| X-IronPort-Anti-Spam-Result | AtseAEuoKlDVcDLgPGdsb2JhbAANOIo7sAIBAQEBN4JUAQEBAQM4QBELGAkWDwkDAgECAQ8iFBMGAgEBh3oDpFiKbw2JToohg3mDHAOLMYhHiBuEQodn |
| X-IronPort-AV | E=Sophos;i="4.77,768,1336341600"; d="scan'208";a="393784454" |
| Date | Tue, 14 Aug 2012 21:40:10 +0200 |
| From | Virgil Stokes <vs@it.uu.se> |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:15.0) Gecko/20120808 Thunderbird/15.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: Strange behavior |
| References | <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com> |
| In-Reply-To | <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com> |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| 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.3285.1344973216.4697.python-list@python.org> (permalink) |
| Lines | 82 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1344973216 news.xs4all.nl 6906 [2001:888:2000:d::a6]:59833 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:27056 |
Show key headers only | View raw
On 2012-08-14 17:38, light1quark@gmail.com wrote:
> Hi, I am migrating from PHP to Python and I am slightly confused.
>
> I am making a function that takes a startingList, finds all the strings in the list that begin with 'x', removes those strings and puts them into a xOnlyList.
>
> However if you run the code you will notice only one of the strings beginning with 'x' is removed from the startingList.
> If I comment out 'startingList.remove(str);' the code runs with both strings beginning with 'x' being put in the xOnlyList.
> Using the print statement I noticed that the second string that begins with 'x' isn't even identified by the function. Why does this happen?
>
> 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!
You might find the following useful:
def testFunc(startingList):
xOnlyList = []; j = -1
for xl in startingList:
if (xl[0] == 'x'):
xOnlyList.append(xl)
else:
j += 1
startingList[j] = xl
if j == -1:
startingList = []
else:
del startingList[j:-1]
return(xOnlyList)
testList1 = ['xasd', 'xjkl', 'sefwr', 'dfsews']
testList2 = ['xasd', 'xjkl', 'xsefwr', 'xdfsews']
testList3 = ['xasd', 'jkl', 'sefwr', 'dfsews']
testList4 = ['asd', 'jkl', 'sefwr', 'dfsews']
xOnlyList = testFunc(testList1)
print 'xOnlyList = ',xOnlyList
print 'testList = ',testList1
xOnlyList = testFunc(testList2)
print 'xOnlyList = ',xOnlyList
print 'testList = ',testList2
xOnlyList = testFunc(testList3)
print 'xOnlyList = ',xOnlyList
print 'testList = ',testList3
xOnlyList = testFunc(testList4)
print 'xOnlyList = ',xOnlyList
print 'testList = ',testList4
And here is another version using list comprehension that I prefer
testList1 = ['xasd', 'xjkl', 'sefwr', 'dfsews']
testList2 = ['xasd', 'xjkl', 'xsefwr', 'xdfsews']
testList3 = ['xasd', 'jkl', 'sefwr', 'dfsews']
testList4 = ['asd', 'jkl', 'sefwr', 'dfsews']
def testFunc2(startingList):
return([x for x in startingList if x[0] == 'x'], [x for x in
startingList if x[0] != 'x'])
xOnlyList,testList = testFunc2(testList1)
print xOnlyList
print testList
xOnlyList,testList = testFunc2(testList2)
print xOnlyList
print testList
xOnlyList,testList = testFunc2(testList3)
print xOnlyList
print testList
xOnlyList,testList = testFunc2(testList4)
print xOnlyList
print testList
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