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


Groups > comp.lang.python > #71559

Re: New to Python. For in loops curiosity

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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; 'pycon': 0.01; 'python,': 0.02; '(at': 0.04; 'lines,': 0.07; 'string': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'all).': 0.16; 'behave': 0.16; 'characters:': 0.16; 'dictionaries': 0.16; 'elements,': 0.16; 'iterated': 0.16; 'iteration': 0.16; 'keys.': 0.16; 'looping': 0.16; 'loops': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'wrote:': 0.18; 'all,': 0.19; 'basically': 0.19; 'things.': 0.19; 'header:User-Agent:1': 0.23; 'decide': 0.24; 'question': 0.24; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'character': 0.29; 'file': 0.32; 'class': 0.32; 'lists': 0.32; 'run': 0.32; '(including': 0.33; 'comment': 0.34; 'something': 0.35; 'objects': 0.35; 'curious': 0.36; 'false': 0.36; 'thanks': 0.36; 'subject:New': 0.37; 'too': 0.37; 'starting': 0.37; 'skip:o 20': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'itself': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'skip:u 10': 0.60; 'new': 0.61; 'simple': 0.61; 'more': 0.64; 'url:bit': 0.65; 'url:ly': 0.65; 'here': 0.66; 'subject:. ': 0.67; 'subject:For': 0.78; 'characters,': 0.84; 'fin': 0.84; 'treating': 0.84
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ned Batchelder <ned@nedbatchelder.com>
Subject Re: New to Python. For in loops curiosity
Date Wed, 14 May 2014 09:41:48 -0400
References <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host 18.189.116.186
User-Agent Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:24.0) Gecko/20100101 Thunderbird/24.5.0
In-Reply-To <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.10006.1400074922.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1400074922 news.xs4all.nl 2829 [2001:888:2000:d::a6]:56390
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:71559

Show key headers only | View raw


On 5/13/14 11:38 PM, Leonardo Petry wrote:
> Hi All,
>
> So I am starting with python and I have been working on some simple exercises.
>
> Here is something I found curious about python loops
>
> This loop run each character in a string
>
> def avoids(word,letters):
> 	flag = True
> 	for letter in letters:
> 		if(letter in word):
> 			flag = False
> 	return flag
>
> The loop below (at the bottom) runs each line of the file
>
> fin = open('wordplay.txt');
> user_input = raw_input('Enter some characters: ')
> count = 0
> for line in fin:
>      word = line.strip()
>      if(avoids(word, user_input)):
>      	count += 1;
>
> This is just too convenient.
> Basically my question is: Why is python not treating the contents of wordplay.txt as one long string and looping each character?
>
> Any comment is greatly appreciate. Thanks
>
>

Every class can decide for itself how it will behave when iterated over 
(including deciding whether it can be iterated at all).   File objects 
produce lines, strings produce characters, lists produce elements, 
dictionaries produce keys.  Other objects do more exotic things.

You might find this helpful:  http://bit.ly/pyiter  It's a PyCon talk 
all about iteration in Python, aimed at new learners.

-- 
Ned Batchelder, http://nedbatchelder.com

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


Thread

New to Python. For in loops curiosity Leonardo Petry <leonardo.petry.br@gmail.com> - 2014-05-13 20:38 -0700
  Re: New to Python. For in loops curiosity John Gordon <gordon@panix.com> - 2014-05-14 03:46 +0000
  Re: New to Python. For in loops curiosity Rustom Mody <rustompmody@gmail.com> - 2014-05-13 20:48 -0700
  Re: New to Python. For in loops curiosity Ian Kelly <ian.g.kelly@gmail.com> - 2014-05-13 21:49 -0600
  Re: New to Python. For in loops curiosity Ben Finney <ben@benfinney.id.au> - 2014-05-14 14:03 +1000
  Re: New to Python. For in loops curiosity Roy Smith <roy@panix.com> - 2014-05-14 07:38 -0400
  Re: New to Python. For in loops curiosity Ned Batchelder <ned@nedbatchelder.com> - 2014-05-14 09:41 -0400

csiph-web