python - 如何检查字符串是否在列表中,使用每个项目上的函数(python3)
问题描述
我正在制作一个类似于 AI 的程序,它会做出响应。我开始编写这个项目,但遇到了障碍。我想获取一个单词列表,然后在返回布尔值时检查给定字符串是否在列表中。我的代码看起来像这样......
listofhellos = ['hello', 'hi', 'hey!!!']
if listofhellos.lower() == 'hello':
# This checks if string 'hello' is in listofhellos
print('Hey, how are you?')
# if it is in the list then print out a response
谢谢你!
解决方案
这是解决方案:
listofhellos = ['hello', 'hi', 'hey!!!']
check = "Hello"
if check.lower() in listofhellos:
print('Exists')
else:
print("Not exists")
推荐阅读
- python - 在特定条件下交换列和行中的值
- python - 如何将嵌套字典完全拆分为与嵌套级别一样多的列?
- javascript - 如何在同一个文件中同时使用类和功能组件
- python-3.x - Django使用按钮重新加载当前页面
- django - docker-compose Vue 连接重置错误
- uicollectionview - 将 CollectionView 嵌入到具有动态高度的 CollectionViewCell 中
- python - 通过网络共享 CX 冻结文件夹
- python - numpy 一维数组和其条目为矩阵的数组之间是否存在单行“点积”?
- electron - 需求未定义 - 电子
- sql - 如何修复 MS Query 中的 SQL 语法?