列出问题 [英] List question
问题描述
您好,
我正在学习python而且不知道为什么会发生这种情况
有人可以解释一下吗?
alist = []
blist = [''one'',''two'','one and two'',''one and four'',''five' ',''一两'']
for bl in:
如果''一'''和''两''在f:
alist.append(f)
for al in alist:
print i
two
一两个
一两
为什么打印第一个两个?
tia
Hello,
I am learning python and dont quuite understand why is this happening
could someone explain?
alist = []
blist = [ ''one'',''two'',''one and two'',''one and four'',''five'',''one two'']
for f in blist:
if ''one'' and ''two'' in f:
alist.append(f)
for i in alist:
print i
two
one and two
one two
why is it printing the first "two"?
tia
推荐答案
2008-03-21 05:16:41 PM, Fr ** @ home.xs4all.nl 写道:
On 2008-03-21 05:16:41 PM, Fr**@home.xs4all.nl wrote:
alist = []
blist = [ ''one'',''two'',''one and two'',''one and four'',''''''''one two'']
for f blist:
i f中的一和两个:
alist.append(f)
for al in alist:
打印i
两个
一个和两个
一个两个
为什么是它打印第一个两个?
alist = []
blist = [ ''one'',''two'',''one and two'',''one and four'',''five'',''one two'']
for f in blist:
if ''one'' and ''two'' in f:
alist.append(f)
for i in alist:
print i
two
one and two
one two
why is it printing the first "two"?
看看这一行:
如果f中的''一个'和''两个': br />
你基本上是这样说的:
if(''one'')和('''''在f中):
这就是为什么你只得到包含''two''的元素。
Ricky
-----开始PGP SIGNATURE -----
版本:GnuPG v1 .4.7(GNU / Linux)
iD8DBQFH5CfQZBKKLMyvSE4RAmDdAJ4hdhYKo7sijC3hlHWGvH 23Z + kswwCfXReH
1kfE + VFM1rfHnXFIDjGGFKU =
= PLdZ
----- END PGP SIGNATURE -----
Look at the this line:
if ''one'' and ''two'' in f:
You''re basically saying:
if (''one'') and (''two'' in f):
which is why you only get elements that contain ''two''.
Ricky
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)
iD8DBQFH5CfQZBKKLMyvSE4RAmDdAJ4hdhYKo7sijC3hlHWGvH 23Z+kswwCfXReH
1kfE+VFM1rfHnXFIDjGGFKU=
=PLdZ
-----END PGP SIGNATURE-----
来自@ home写道:
From@home writes:
如果'一'和''两''在f中:
alist.append(f)
if ''one'' and ''two'' in f:
alist.append(f)
使用:
如果f中的一个和f中的两个:...
Use:
if ''one'' in f and ''two'' in f: ...
if(''一个'',''两个')在f:...
" are"在Python 2.5中给出了一个错误,其中包含了from future import *
语句。你在运行什么版本和平台。另外,
文档没有提到它。
http://docs.python.org/ref/keywords.html
if (''one'', ''two'') are in f: ...
"are" gives me an error in Python 2.5 with a "from future import *"
statement included. What version and platform are you running. Also,
the docs don''t mention it.
http://docs.python.org/ref/keywords.html
这篇关于列出问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!