python - if item == "If" 和 if 'If' in item 的区别?
问题描述
我的代码在这里
for index, item in enumerate(actdiagram, start=1):
print(item+"&")
# if item == "If":
if 'If' in item:
os.write(fd, ("if").encode('ascii'))
indexOfIf = index
elif index == (indexOfIf+1):
os.write(fd, ("\"" + item + "\" then \n").encode('ascii'))
else:
os.write(fd, ("-->" + item + "\n").encode('ascii'))
我的问题是我的代码没有通过, if item == "If"
但它工作正常,因为if 'If' in item:
我想知道为什么会这样。
解决方案
==
检查两件事是否相同,而 asin
检查第二件事是否包含第一件事。
>>> "foo" in "blah blah foo blah blah"
True
>>> "foo" == "blah blah foo blah blah"
False
这里,foo
是在第二个字符串中,但它还包含其他内容。
在您的情况下,它看起来item
不是 string "If"
,但它确实包含它。
推荐阅读
- visual-studio-code - 在 vscode 中使用 prettier 时如何在我的代码中放置水龙头空间
- docker - 如何将 file_system_blacklist arg 传递给 Datadog Docker 代理运行命令?
- python - 从子列表中随机选择以填充空子列表
- shell - 无法在我的 shell 脚本中使用 perl 命令搜索和替换文件
- apache-kafka - 系统架构——ETL
- javascript - jQuery 异步 ajax 调用
- c++ - 如何使用 C++ 中的 Builder 模式返回类的实例?
- xrp - 如何签署 JSON 交易?
- ajax - “检测到非法选择...”动态下拉选择列表错误 I Drupal8
- javascript - 在我的 React 应用程序中,如何在这个 map 函数中的每个项目之后添加一个换行符?