相关疑难解决方法(0)

将字符串转换为单词列表?

我正在尝试使用python将字符串转换为单词列表.我想采取以下内容:

string = 'This is a string, with words!'
Run Code Online (Sandbox Code Playgroud)

然后转换为这样的东西:

list = ['This', 'is', 'a', 'string', 'with', 'words']
Run Code Online (Sandbox Code Playgroud)

请注意省略标点符号和空格.最快的方法是什么?

python string words list text-segmentation

58
推荐指数
7
解决办法
17万
查看次数

使用For循环将字符串中的单词更改为列表项

我正在尝试使用for循环来查找包含正好一个字母e的字符串中的每个单词.

我的猜测是,我需要使用一个for循环,第一串中的每个单词分成了自己的列表(例如,this is a string['this'],['is'],['a'],['string'])

然后,我可以使用另一个For循环来检查每个单词/列表.

我的字符串存储在变量笑话中.

我在构造For循环时遇到了麻烦,无法将每个单词放入自己的列表中.有什么建议?

j2 = []

for s in joke:
if s[0] in j2:
    j2[s[0]] = joke.split()
else:
    j2[s[0]] = s[0]
print(j2)
Run Code Online (Sandbox Code Playgroud)

python string for-loop list

1
推荐指数
1
解决办法
1134
查看次数

标签 统计

list ×2

python ×2

string ×2

for-loop ×1

text-segmentation ×1

words ×1