Re: why for loop print only once after add if statement

2016-05-28 Thread meInvent bbird
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?

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why for loop print only once after add if statement

2016-05-28 Thread meInvent bbird
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?

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why for loop print only once after add if statement

2016-05-28 Thread Steven D'Aprano
On Sat, 28 May 2016 08:19 pm, jobmatt...@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

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why for loop print only once after add if statement

2016-05-28 Thread Peter Otten
jobmatt...@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?

-- 
https://mail.python.org/mailman/listinfo/python-list


Re: why for loop print only once after add if statement

2016-05-28 Thread Sayth Renshaw
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
-- 
https://mail.python.org/mailman/listinfo/python-list


why for loop print only once after add if statement

2016-05-28 Thread jobmattcon
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?

-- 
https://mail.python.org/mailman/listinfo/python-list