Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #27063

Re: Strange behavior

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.002
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'languages,': 0.04; 'python': 0.09; '[];': 0.09; 'compact': 0.09; 'notation': 0.09; 'looked': 0.10; 'def': 0.10; 'aug': 0.13; '"list': 0.16; '#this': 0.16; '(note': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'parentheses': 0.16; 'wed,': 0.16; 'wrote:': 0.17; 'code.': 0.20; 'putting': 0.20; 'received:209.85.214.174': 0.21; 'example': 0.23; '15,': 0.23; "python's": 0.23; 'seems': 0.23; 'idea': 0.24; 'header:In-Reply-To:1': 0.25; 'am,': 0.27; 'language.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'chris': 0.28; 'statements': 0.29; 'str': 0.29; 'compatible': 0.30; 'print': 0.32; 'docs': 0.33; 'function.': 0.33; 'shift': 0.33; 'problem': 0.33; 'to:addr:python-list': 0.33; 'another': 0.33; 'version': 0.34; 'changed': 0.34; 'received:google.com': 0.34; 'list': 0.35; 'awesome': 0.35; 'built-in': 0.35; "won't": 0.35; 'received:209.85': 0.35; 'list.': 0.35; 'but': 0.36; 'functional': 0.36; "i'll": 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'called': 0.39; 'header:Received:5': 0.40; 'your': 0.60; 'side': 0.61; 'worth': 0.63; 'more': 0.63
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=ot2rUw89FtuoBHCp4Q77EwKjGQiWxottZxaH0dveCcE=; b=w8OjGwh1vBpU87wzutaXTnmNjg3YkyDjB+u6V4p1ptQz8r7uYvFhrlzw/Zf7HahcEj rA/P4iiTIxXsu5SWsqIVL0X6qxwrl/dx+CkBm5ZJhU8+r43zVnEZiZpLvRp3LS8X7tQO /gqPfQx4vLjpfPfofHl5B9Wuae4L0atUJEilVGBUIRvXFRTmY7oVjkf48zTuNamIFXCl Yr/NHRbEl9dRb+4nDZTtzZpEpQTMhfYIc2ETK5E3JjExIwkZk9hmM8sj3YsmeG9rFKjl kt1R4L9izLG34xkZnDNHyjELDRwalahmwo2LpPq15SBjA09FnDJs4PNnHmF5wOhlOVc3 qyTg==
MIME-Version 1.0
In-Reply-To <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com>
References <1a1834ae-2b4a-473f-b626-f37a17588199@googlegroups.com>
Date Wed, 15 Aug 2012 07:55:58 +1000
Subject Re: Strange behavior
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
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.3289.1344981361.4697.python-list@python.org> (permalink)
Lines 39
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1344981361 news.xs4all.nl 6877 [2001:888:2000:d::a6]:36497
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:27063

Show key headers only | View raw


On Wed, Aug 15, 2012 at 1:38 AM,  <light1quark@gmail.com> wrote:
> 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'])

Other people have explained the problem with your code. I'll take this
example as a way of introducing you to one of Python's handy features
- it's an idea borrowed from functional languages, and is extremely
handy. It's called the "list comprehension", and can be looked up in
the docs under that name,

def testFunc(startingList):
    xOnlyList = [strng for strng in startingList if strng[0] == 'x']
    startingList = [strng for strng in startingList if strng[0] != 'x']
    print(xOnlyList)
    print(startingList)

It's a compact notation for building a list from another list. (Note
that I changed "str" to "strng" to avoid shadowing the built-in name
"str", as others suggested.)

(Unrelated side point: Putting parentheses around the print statements
makes them compatible with Python 3, in which 'print' is a function.
Unless something's binding you to Python 2, consider working with the
current version - Python 2 won't get any more features added to it any
more.)

Python's an awesome language. You may have to get your head around a
few new concepts as you shift thinking from PHP's, but it's well worth
while.

Chris Angelico

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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