您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

使用特定单词列表将单词转换为数字的最佳方法

使用特定单词列表将单词转换为数字的最佳方法

from string import punctuation as pnc
tokens = {':)', 'cool', 'happy', 'fun'}
tweets = ['this has been a fun day :)', 'i find python cool! it makes me happy']
for tweet in tweets:
    s = [(word in tokens or word.strip(pnc) in tokens) for word in tweet.split()]
    print(' '.join('1' if t else '0' for t in s))

输出

0 0 0 0 1 0 1
0 0 0 1 0 0 0 1

如@EOL所示,or第四行中的可以处理:)

仍然存在无法正确处理的情况,例如使用cool :), I like it。问题是需求所固有的。

其他 2022/1/1 18:49:00 有430人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶