Python:从字符串中获取单个单词



我正在尝试用python制作一个字符串分析器。我以这个输入为例:

toAnalyze= "Hello!!gyus-- loln"作为输出,我想要这样的东西:

>Output: ['Hello', '!!', 'guys', '--', ' ', 'lol']我希望每一只章鱼都按原始订单排序

我曾想过扫描原始字符串中的所有字符;\n〃;character和我想出了这个解决方案:

toAnalyze= "Hello!!gyus-- loln"
final = ""
for char in toAnalyze:
if char != " nt" and char != " " and char != "n" and char != "nt":
final += char
elif char == " " or char == "n" or char == "nt" or char == " nt":
if not final.isalnum():
word= ""
thing = ""
for l in final:
if l.isalnum():
word += l
else:
thing += l
print("word: " + word)
print("thing: " + thing )

我目前的输出是:

>Output: thing: !!-- word: Hellogyus lol

你有想法吗?想要的输出:

>Output: ['Hello', '!!', 'guys', '--', ' ', 'lol']

提前感谢,祝今天愉快

我不是一个喜欢python的人,但我想帮助你入门。这是一个有效的解决方案,你可以尝试改进它,让它变得更像蟒蛇:

toAnalyze= 'Hello!!gyus-- loln'
word = ''
separator = ''
tokens = []
for ch in toAnalyze:
if ch.isalnum():
word += ch

# we met the first character of a separator, so save a word
if not ch.isalnum() and word:
tokens.append(word)
word = ''

# 1. we met the first alphanumeric after a separator, so save the separator or
# 2. we met a new separator right after another one, also save the old separator
if ch.isalnum() and separator or separator and separator[-1] != ch:
tokens.append(separator)
separator = ''

if not ch.isalnum():
separator += ch

示例的输出是:

['Hello', '!!', 'gyus', '--', ' ', 'lol']

相关内容

  • 没有找到相关文章

最新更新