【发布时间】:2021-04-19 00:54:13
【问题描述】:
我有一个常见的操作,允许用户使用“word”或“char”作为单位,我一直这样做:
def split(s, unit):
return s.split() if unit == 'word' else list(s)
用法:
>>> foo = "this is a foo bar sentence"
>>> split(foo, 'word')
['this', 'is', 'a', 'foo', 'bar', 'sentence']
>>> split(foo, 'char')
['t', 'h', 'i', 's', ' ', 'i', 's', ' ', 'a', ' ', 'f', 'o', 'o', ' ', 'b', 'a', 'r', ' ', 's', 'e', 'n', 't', 'e', 'n', 'c', 'e']
有没有一种简单/更好的方法来创建一个根据单词(由str.split 定义)和字符分割输入字符串的函数?
【问题讨论】:
标签: python string split nlp token