python substring匹配问题和 /或逻辑
我开始学习Python并编写一个具有以下逻辑的小脚本,并且可以理解输出或获得结果的原因。 我有以下代码:
test = ['aa01', 'zz02', '01']
for element in test:
print(f'{element}')
if 'aa' or 'zz' in element:
print('Condition met')
else:
print('Condition not met')
我将获得以下输出:
aa01
Condition met
zz02
Condition met
01
Condition met
使用此代码行:
if 'aa' or 'zz' in element:
我希望列表中的每个元素都会寻找一个子字符串匹配(“ aa''或'zz'),然后在满足条件时打印。 基于输出,我不明白为什么第三个元素与该条件匹配,而该条件均不具有substring。
如果我执行以下代码:
'zz' in 'aa01'
False
和以下代码:
'zz' or 'aa' in 'aa'
'zz'
在此阶段,我不在substring匹配的工作方式,即“ AA”中没有“ ZZ”。 “ ZZ”不在“ AA”中吗?
我还尝试了遵循代码,这也没有给我带来理想的结果,至少我根据对Python的理解而期望的。
test = ['aa01', 'zz02', '01']
for element in test:
print(f'{element}')
if 'aa' in element:
print('Condition met')
if 'zz' in element:
print('Condition met')
else:
print('Condition not met')
这是我要获得的输出:
aa01
Condition met
Condition not met
zz02
Condition met
01
Condition not met
因此,上面的AA01如何显示状态满足,并且同时显示未满足条件。
我无法解释我看到的行为。希望有人向我解释一下,或者指出有关此信息的一些信息。
非常感谢您的帮助。
I am starting to learn Python and writing a small script with the following logic and can understand the reason for the output or the results I am getting.
I have following code:
test = ['aa01', 'zz02', '01']
for element in test:
print(f'{element}')
if 'aa' or 'zz' in element:
print('Condition met')
else:
print('Condition not met')
I get following output:
aa01
Condition met
zz02
Condition met
01
Condition met
With this line of code:
if 'aa' or 'zz' in element:
I am expecting for each element in the list to look for a substring match (either 'aa' or 'zz') and then print if the condition is met.
Based on the output I do not understand why the third element is matching this condition which does not have neither of the substring.
If I execute following code:
'zz' in 'aa01'
False
and following code:
'zz' or 'aa' in 'aa'
'zz'
At this stage I am not how the substring matching is working, i.e. there is no 'zz' in 'aa'.
Is it returning that 'zz' is not in 'aa'?
I also tried following code, which is also not giving me the desired result, well at least what I am expecting based on my understanding of Python so far.
test = ['aa01', 'zz02', '01']
for element in test:
print(f'{element}')
if 'aa' in element:
print('Condition met')
if 'zz' in element:
print('Condition met')
else:
print('Condition not met')
This is the output i am getting:
aa01
Condition met
Condition not met
zz02
Condition met
01
Condition not met
So how is aa01 above showing Condition met and also at the same time showing Condition not met.
I cannot explain the behavior I am seeing. Would like someone to please explain this to me or point at some information on this.
Thanks a lot for help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那条线没有做您认为正在做的事情。
当您在该中编写
时,它会检查是否
包含此
。如果您只编写,则如果是:
,则只是一个真实的检查,除''
空字符串外,在字符串上始终返回true。因此,当您在此操作中运行时,您实际在做的是“如果是”和“如果在同一行中”。
为了使行做您期望的事情应该像这样写:
That line isn't doing what you think it is doing.
When you write
if this in that
it checks to see ifthat
containsthis
. If you just writeif this:
then it is just a truthy check which will always return True on strings except for the''
empty string.So when you run
if this or that in those:
, what you are actually doing is asking 'if this' and 'if that in those' on the same line.To make the line do what you expect it should be written like this: