Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #71526 > unrolled thread
| Started by | Leonardo Petry <leonardo.petry.br@gmail.com> |
|---|---|
| First post | 2014-05-13 20:38 -0700 |
| Last post | 2014-05-14 09:41 -0400 |
| Articles | 7 — 7 participants |
Back to article view | Back to comp.lang.python
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
| From | Leonardo Petry <leonardo.petry.br@gmail.com> |
|---|---|
| Date | 2014-05-13 20:38 -0700 |
| Subject | New to Python. For in loops curiosity |
| Message-ID | <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com> |
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
[toc] | [next] | [standalone]
| From | John Gordon <gordon@panix.com> |
|---|---|
| Date | 2014-05-14 03:46 +0000 |
| Message-ID | <lkuoum$2eq$1@reader1.panix.com> |
| In reply to | #71526 |
In <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com> Leonardo Petry <leonardo.petry.br@gmail.com> writes:
> 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
Your code is not treating the contents of wordplay.txt as one long string
because 'for line in fin:' tells it to read line-by-line.
If you want to read the entire contents, use the read() method:
file_content = fin.read()
--
John Gordon Imagine what it must be like for a real medical doctor to
gordon@panix.com watch 'House', or a real serial killer to watch 'Dexter'.
[toc] | [prev] | [next] | [standalone]
| From | Rustom Mody <rustompmody@gmail.com> |
|---|---|
| Date | 2014-05-13 20:48 -0700 |
| Message-ID | <a651ad84-c4b9-4824-87d3-cb35d920fc77@googlegroups.com> |
| In reply to | #71526 |
On Wednesday, May 14, 2014 9:08:32 AM UTC+5:30, Leonardo Petry wrote: > > 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? Did you mean convenient or inconvenient? Anyways... Maybe you want the read method? https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2014-05-13 21:49 -0600 |
| Message-ID | <mailman.9988.1400039394.18130.python-list@python.org> |
| In reply to | #71526 |
On Tue, May 13, 2014 at 9:38 PM, Leonardo Petry
<leonardo.petry.br@gmail.com> wrote:
> 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?
Because the iterator for file-like objects iterates over lines, not characters.
[toc] | [prev] | [next] | [standalone]
| From | Ben Finney <ben@benfinney.id.au> |
|---|---|
| Date | 2014-05-14 14:03 +1000 |
| Message-ID | <mailman.9990.1400040202.18130.python-list@python.org> |
| In reply to | #71526 |
Leonardo Petry <leonardo.petry.br@gmail.com> writes:
> So I am starting with python and I have been working on some simple
> exercises.
You are welcome here. Congratulations on starting with Python!
You may also be interested to know there is also a separate forum
<URL:https://mail.python.org/mailman/listinfo/tutor> dedicated to
tutoring beginners in Python.
> 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
You should avoid using U+0009 TAB characters for indentation, since they
render inconsistently and can easily result in invisible differences in
changed code. Instead, use four-column indentation with space (U+0020
SPACE) characters.
> 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.
Is that a complaint, or shock at how easy it is? :-)
> Basically my question is: Why is python not treating the contents of
> wordplay.txt as one long string and looping each character?
Because the types of the objects are different; different types define
different behaviour. Indeed, that is almost the definition of what a
type is.
A text string object supports iteration by returning each character
<URL:https://docs.python.org/3/library/stdtypes.html#text-sequence-type-str>.
A file object supports iteration by returning each line from the stream
<URL:https://docs.python.org/3/library/io.html>.
--
\ “Our products just aren't engineered for security.” —Brian |
`\ Valentine, senior vice-president of Microsoft Windows |
_o__) development, 2002 |
Ben Finney
[toc] | [prev] | [next] | [standalone]
| From | Roy Smith <roy@panix.com> |
|---|---|
| Date | 2014-05-14 07:38 -0400 |
| Message-ID | <roy-B8E824.07382614052014@news.panix.com> |
| In reply to | #71526 |
In article <2f08e970-1334-4e7f-ba84-14869708a73b@googlegroups.com>,
Leonardo Petry <leonardo.petry.br@gmail.com> wrote:
> Basically my question is: Why is python not treating the contents of
> [a file] as one long string and looping each character?
Because whoever designed the original file object decided that the right
way to iterate over a file is line by line. In Python (although, at
this level of explanation, I could be describing pretty much any
language which has iterators), there is an iterator protocol which
implements two ideas:
1) There's a method to call to get the next item.
2) There's a way for that method to signal that you've reached the end.
Exactly what "the next item" means is up to whoever implements the
iterator. In this case, it was decided that the most convenient thing
would be for "item" to mean "line". If you really want to iterate over
a file character-by-character, it's easy enough to write an adapter.
Something like this (untested):
def getchar(f):
for line in f:
for c in line:
yield c
Of course, if the native file iterator was character-by-character, then
if you wanted it line-by-line, you would have to write the inverse, a
function which accumulates characters until it sees a newline, and then
returns that. Neither one is fundamentally better, or more correct than
the other. One may just be more convenient for a particular use case.
[toc] | [prev] | [next] | [standalone]
| From | Ned Batchelder <ned@nedbatchelder.com> |
|---|---|
| Date | 2014-05-14 09:41 -0400 |
| Message-ID | <mailman.10006.1400074922.18130.python-list@python.org> |
| In reply to | #71526 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web