Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #109199 > unrolled thread
| Started by | jobmattcon@gmail.com |
|---|---|
| First post | 2016-05-28 03:19 -0700 |
| Last post | 2016-05-28 12:59 -0400 |
| Articles | 7 — 6 participants |
Back to article view | Back to comp.lang.python
why for loop print only once after add if statement jobmattcon@gmail.com - 2016-05-28 03:19 -0700
Re: why for loop print only once after add if statement Sayth Renshaw <flebber.crue@gmail.com> - 2016-05-28 03:49 -0700
Re: why for loop print only once after add if statement Peter Otten <__peter__@web.de> - 2016-05-28 13:54 +0200
Re: why for loop print only once after add if statement Steven D'Aprano <steve@pearwood.info> - 2016-05-28 22:02 +1000
Re: why for loop print only once after add if statement meInvent bbird <jobmattcon@gmail.com> - 2016-05-28 05:31 -0700
Re: why for loop print only once after add if statement meInvent bbird <jobmattcon@gmail.com> - 2016-05-28 05:35 -0700
Re: why for loop print only once after add if statement Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2016-05-28 12:59 -0400
| From | jobmattcon@gmail.com |
|---|---|
| Date | 2016-05-28 03:19 -0700 |
| Subject | why for loop print only once after add if statement |
| Message-ID | <4743c1df-de66-44e1-9d27-b62582d757ac@googlegroups.com> |
for item, i in enumerate(aa)
print item
this writing, it can print all values
for item, i in enumerate(aa)
if item == findit:
print item
this only print the first value, means it only print once then not print again,
where is wrong?
[toc] | [next] | [standalone]
| From | Sayth Renshaw <flebber.crue@gmail.com> |
|---|---|
| Date | 2016-05-28 03:49 -0700 |
| Message-ID | <236d89d4-7684-4a83-9367-3fb226a1fcf4@googlegroups.com> |
| In reply to | #109199 |
On Saturday, 28 May 2016 20:19:23 UTC+10, meInvent bbird wrote: > for item, i in enumerate(aa) > print item > > this writing, it can print all values > > for item, i in enumerate(aa) > if item == findit: > print item > > this only print the first value, means it only print once then not print again, > > where is wrong? Enumerate is there to get rid of the i its not need enumerate is a generator. list(enumerate(aa, start=1)) https://docs.python.org/3/library/functions.html#enumerate In [1]: aa = range(10) In [2]: list(enumerate(aa, start=1)) Out[2]: [(1, 0), (2, 1), (3, 2), (4, 3), (5, 4), (6, 5), (7, 6), (8, 7), (9, 8), (10, 9)] Sayth
[toc] | [prev] | [next] | [standalone]
| From | Peter Otten <__peter__@web.de> |
|---|---|
| Date | 2016-05-28 13:54 +0200 |
| Message-ID | <mailman.7.1464436471.1839.python-list@python.org> |
| In reply to | #109199 |
jobmattcon@gmail.com wrote:
> for item, i in enumerate(aa)
> print item
>
> this writing, it can print all values
>
> for item, i in enumerate(aa)
> if item == findit:
> print item
>
> this only print the first value, means it only print once then not print
> again,
Assuming
aa = ["foo", "bar", "baz"]
and
findit = "bar"
the print statement will only be executed when findit == item.
First iteration of your for loop:
item = "foo"
if "foo" == "bar": # False
print "foo" # not executed, nothing printed
Second iteration:
item = "bar"
if "bar" == "bar": # True
print "bar" # prints bar
Third iteration:
item = "baz"
if "baz" == "bar": # False
print "baz" # not executed, nothing printed
> where is wrong?
This depends on what you want to achieve. Can you tell us?
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-05-28 22:02 +1000 |
| Message-ID | <574988f5$0$1614$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #109199 |
On Sat, 28 May 2016 08:19 pm, jobmattcon@gmail.com wrote:
> for item, i in enumerate(aa)
> print item
Wrong way around. It should be:
for i, item in enumerate(aa):
print item
For example:
py> for i, item in enumerate(aa):
... print i, item
...
0 c
1 h
2 e
3 e
4 s
5 e
> this writing, it can print all values
>
> for item, i in enumerate(aa)
> if item == findit:
> print item
>
> this only print the first value, means it only print once then not print
> again,
No, it doesn't not print the first value, it prints any value that equals
findit, whatever that is. If nothing equals findit, nothing will be
printed. Only items which equal findit will be printed.
py> findit = 'z'
py> for i, item in enumerate(aa):
... if item == findit:
... print i, item
...
py> findit = 'e'
py> for i, item in enumerate(aa):
... if item == findit:
... print i, item
...
2 e
3 e
5 e
--
Steven
[toc] | [prev] | [next] | [standalone]
| From | meInvent bbird <jobmattcon@gmail.com> |
|---|---|
| Date | 2016-05-28 05:31 -0700 |
| Message-ID | <63664a77-08ba-4083-b34f-9e79d242ca22@googlegroups.com> |
| In reply to | #109199 |
thanks, i discover that i misunderstand i and item, they should be swapped On Saturday, May 28, 2016 at 6:19:23 PM UTC+8, meInvent bbird wrote: > for item, i in enumerate(aa) > print item > > this writing, it can print all values > > for item, i in enumerate(aa) > if item == findit: > print item > > this only print the first value, means it only print once then not print again, > > where is wrong?
[toc] | [prev] | [next] | [standalone]
| From | meInvent bbird <jobmattcon@gmail.com> |
|---|---|
| Date | 2016-05-28 05:35 -0700 |
| Message-ID | <522bb8ef-8f15-4055-8ffc-9431a5110865@googlegroups.com> |
| In reply to | #109199 |
when read my code again, i discover this error i fixed before but forget to do for another branch of if statement now fixed On Saturday, May 28, 2016 at 6:19:23 PM UTC+8, meInvent bbird wrote: > for item, i in enumerate(aa) > print item > > this writing, it can print all values > > for item, i in enumerate(aa) > if item == findit: > print item > > this only print the first value, means it only print once then not print again, > > where is wrong?
[toc] | [prev] | [next] | [standalone]
| From | Dennis Lee Bieber <wlfraed@ix.netcom.com> |
|---|---|
| Date | 2016-05-28 12:59 -0400 |
| Message-ID | <mailman.11.1464454750.1839.python-list@python.org> |
| In reply to | #109199 |
On Sat, 28 May 2016 03:19:10 -0700 (PDT), jobmattcon@gmail.com declaimed
the following:
>for item, i in enumerate(aa)
> print item
>
>this writing, it can print all values
>
>for item, i in enumerate(aa)
> if item == findit:
> print item
>
>this only print the first value, means it only print once then not print again,
>
>where is wrong?
First thought...
for i, item in enumerate(aa):
The first term is the (zero-base) index and the second is the actual
item value.
No idea what "findit" is, so can't do any further testing, nor what
"aa" contains.
>>> aa = "alcoholics anonymous"
>>> for stuff in enumerate(aa):
... print stuff
...
(0, 'a')
(1, 'l')
(2, 'c')
(3, 'o')
(4, 'h')
(5, 'o')
(6, 'l')
(7, 'i')
(8, 'c')
(9, 's')
(10, ' ')
(11, 'a')
(12, 'n')
(13, 'o')
(14, 'n')
(15, 'y')
(16, 'm')
(17, 'o')
(18, 'u')
(19, 's')
>>>
>>> for i, v in enumerate(aa):
... if v == "o":
... print i
...
3
5
13
17
>>>
--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com HTTP://wlfraed.home.netcom.com/
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web