- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我想用“HAPPY”替换所有快乐的表情符号,反之亦然用“SAD”替换文本文件的悲伤表情符号。但是代码无法正常工作。虽然它检测到笑脸(截至目前 :-)),但在下面的示例中它没有用文本替换表情符号,它只是附加文本并且由于我似乎不理解的原因它也附加了两次。
dict_sad={":-(":"SAD", ":(":"SAD", ":-|":"SAD", ";-(":"SAD", ";-<":"SAD", "|-{":"SAD"}
dict_happy={":-)":"HAPPY",":)":"HAPPY", ":o)":"HAPPY",":-}":"HAPPY",";-}":"HAPPY",":->":"HAPPY",";-)":"HAPPY"}
#THE INPUT TEXT#
a="guys beautifully done :-)"
for i in a.split():
for j in dict_happy.keys():
if set(j).issubset(set(i)):
print "HAPPY"
continue
for k in dict_sad.keys():
if set(k).issubset(set(i)):
print "SAD"
continue
if str(i)==i.decode('utf-8','replace'):
print i
输入文本
a="guys beautifully done :-)"
输出(“HAPPY”来了两次,表情也没有消失)
guys
-
beautifully
done
HAPPY
HAPPY
:-)
预期输出
guys
beautifully
done
HAPPY
最佳答案
你正在将每个单词和每个表情符号变成一个集合;这意味着您正在寻找单个字符的重叠。您可能最多希望使用完全匹配:
for i in a.split():
for j in dict_happy:
if j == i:
print "HAPPY"
continue
for k in dict_sad:
if k == i:
print "SAD"
continue
您可以直接遍历字典,无需在那里调用.keys()
。您实际上并没有使用字典值;你可以这样做:
for word in a.split():
if word in dict_happy:
print "HAPPY"
if word in dict_sad:
print "SAD"
然后也许使用集合而不是字典。这可以简化为:
words = set(a.split())
if dict_happy.viewkeys() & words:
print "HAPPY"
if dict_sad.viewkeys() & words:
print "SAD"
使用 dictionary view在键上作为一组。尽管如此,还是使用集合会更好:
sad_emoticons = {":-(", ":(", ":-|", ";-(", ";-<", "|-{"}
happy_emoticons = {":-)", ":)", ":o)", ":-}", ";-}", ":->", ";-)"}
words = set(a.split())
if sad_emoticons & words:
print "HAPPY"
if happy_emoticons & words:
print "SAD"
如果你想从文本中删除表情符号,你必须过滤这些词:
for word in a.split():
if word in dict_happy:
print "HAPPY"
elif word in dict_sad:
print "SAD"
else:
print word
或者更好的是,结合两个字典并使用 dict.get()
:
emoticons = {
":-(": "SAD", ":(": "SAD", ":-|": "SAD",
";-(": "SAD", ";-<": "SAD", "|-{": "SAD",
":-)": "HAPPY",":)": "HAPPY", ":o)": "HAPPY",
":-}": "HAPPY", ";-}": "HAPPY", ":->": "HAPPY",
";-)": "HAPPY"
}
for word in a.split():
print emoticons.get(word, word)
这里我传入了当前单词作为查找键和默认值;如果当前单词不是表情符号,则打印单词本身,否则打印单词 SAD
或 HAPPY
。
关于python - 用 "SAD"或 "HAPPY"替换表情符号的代码无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26969747/
为大家分享一组极度闷骚的讨红包表情,聊天发状态可以有!讨红包的主人公是一只嫉妒闷骚的绿皮青蛙,名为sad frog,希望大家会喜欢。 &
平安夜没人约,圣诞节没人约,今后的节日都没人约,反倒是得看情侣们在网上各种秀恩爱,人生如此艰难,为何要虐单身狗呢? &nb
我正在实现一个 JAVA 程序来查找不同图像帧之间的相似性。目前我正在使用两种不同的算法来实现这一目标。基本上我将图像划分为 n*n 像素 block 并计算 SAD为他们进行比较。现在我的主要问题是
我有文本文件: # How often update data period=30 #For Location location=London.uk 我想在标准输出上打印: London.uk 当我使
我正在尝试编写 16 个 uint8_t 大小输入的绝对差和的 NEON 版本: inline static int f_sad_16(const uint8_t* a, const uint8_t*
我对 ARM 和 NEON 编程相当陌生,我被赋予了优化 SAD(绝对差和)函数的任务。我不知道从哪里开始,我尝试了几种生成 NEON 代码的方法但没有成功。顺序函数看起来像这样: void sad_
所以我想用“HAPPY”替换所有快乐的表情符号,反之亦然用“SAD”替换文本文件的悲伤表情符号。但是代码无法正常工作。虽然它检测到笑脸(截至目前 :-)),但在下面的示例中它没有用文本替换表情符号,它
如果我们从 SAD 算法计算出视差值,我们如何使用该视差值来制作视差图?他们有什么方法吗? 最佳答案 当您使用 SAD 算法计算视差值时,您会得到每个像素的视差值。假设您有两张尺寸为 100x100(
我有两个 uint8_t 数组,它们都有 64 个元素。我想出的“最佳”方法是加载 4x 16 个元素,将它们放入两个 m128i 寄存器,然后将它们都放入一个 m256 寄存器。这是为两个 uint
网络应用在这里: http://www.digitaltransitions.com/visualizer/visualizer.html 主要的javascript在这里: http://www.d
我刚开始学习 Python 几天,所以请原谅我的粗暴工作。 def sum_digits(n): s = 0 while n: s += n % 10
我是一名优秀的程序员,十分优秀!