python - Python - 如果一个“好”字和一个“坏”字包含在一个字母中,打印出来
问题描述
我有一个坏词列表和一个好词列表。我的想法是首先搜索好词,然后查看字母中的哪个含义包含这些好词,但如果该含义还包含一个坏词 - 那么它不应该打印出含义。
ETC:
Good word: "Lo", "Yes"
Bad word: "Hate", "Not"
text_roman: "Hello guys. My name is Lo and I hate to code :')"
含义:“大家好。我的名字是 Lo,我讨厌编码 :')”<--“只是个玩笑!!
所以意思如果它搜索那个意思,它应该告诉我们有一个包含好的意思,然后检查它是否包含坏词。如果是这样,那么我们不想打印出含义,但如果它不包含任何坏词,那么我们应该将其打印出来。
我尝试编码的方式是:
text_roman = "Hello guys. My name is Lo and I hate to code :')"
good_word = ["Lo", "Yes"]
bad_word = ["Hate", "Not"]
for text in good_word:
if text in text_roman:
print("Yay found word " + text_roman)
for bad_text in bad_word:
if bad_text not in text_roman:
print("Yay no bad words!")
当我尝试这个时,不幸的是,输出也给出了所有包含坏词的词
解决方案
我会先遍历那些不好的词,然后跳过它们。然后,如果没有跳过,检查一个好词
good_word = ["Lo", "Yes"]
bad_word = ["Hate", "Not"]
has_bad = False
for b in bad_word:
if b in text_roman:
has_bad = True
continue
for g in good_word:
if g in text_roman:
print("Yay found word " + g + " in text_roman")
if not has_bad:
print("Yay no bad words!")
注意:in
区分大小写,因此"Hate" in "I hate case-sensitivity"
为 False
推荐阅读
- java - java中的google-api-client依赖项不起作用
- linux - 检测 /dev/tty 是否可用和功能的跨平台方法
- business-process-management - Appian RPA 进程未运行
- python - 如何使用 pandas 将我的 INT 值从 int32 设置为 int64
- c# - System.Diagnostics.EventLog 不包含正确的消息
- r - 使用 R 根据条件随机播放数据帧中的行
- julia - MethodError:无法将 Array{Float64,1} 类型的对象“转换”为 SentinelArrays.ChainedVector{Float64,Array{Float64,1}} 类型的对象
- c# - Azure EventHubs 抛出异常:端点的至少一个接收器是使用 '0' 的纪元创建的,因此不允许使用非纪元接收器
- python - Django 表单继承不从字段中读取值
- javascript - javascript 从一个对象数组拆分为 2 个匹配项和不匹配项的数组