it.strip() !=""]
Don't use comprehensions for side effects. To get what you want, just
write normal code.
for it in x:
if it.strip() != '':
print('ok')
ok
ok
ok
[None, None, None]
i understand there are three 'ok' in the output,but why i have the
output of [None, None, None]
Reread the ref manual section on comprehensions.
--
Terry Jan Reedy
--
https://mail.python.org/mailman/listinfo/python-list
>>> [print("ok") for it in x if it.strip() !=""]
ok
ok
ok
[None, None, None]
i understand there are three 'ok' in the output,but why i have the
output of [None, None, None]
--
Johannes Schneider
Webentwicklung
johannes.schnei...@galileo-press.d
length power writes:
> >>> x=['','x1','x2','x3',' ']
> >>> x
> ['', 'x1', 'x2', 'x3', ' ']
> >>> [print("ok") for it in x if it.strip() !="
This is called imperative programming:
for it in x:
... if it.strip() != '':
...print ("Ok")
This is called functional programming:
>>> [y for y in x if y.strip() != '']
['x1', 'x2', 'x3']
What you have is a confusion:
print is imperative
comprehension is functional
You should not mix
7;x2','x3',' ']
>>>> x
> ['', 'x1', 'x2', 'x3', ' ']
>>>> [print("ok") for it in x if it.strip() !=""]
> ok
> ok
> ok
> [None, None, None]
>
> i understand there
>>> x=['','x1','x2','x3',' ']
>>> x
['', 'x1', 'x2', 'x3', ' ']
>>> [print("ok") for it in x if it.strip() !=""]
ok
ok
ok
[None, None, None]
i understand there are three 'ok' in the output,but why i have the output
of [None, None, None]
--
https://mail.python.org/mailman/listinfo/python-list