所以,我试图找出获取字符串列表并将其转换为单词列表的最佳方法。我还想从字符串中删除所有标点符号。我的思考过程是:
从字符串列表到单词列表,这似乎需要很多步骤。有没有人有更简洁的方法或可以对我的过程提出建议?最终目标是将字符串列表传递给计数器类以找到最常见的单词。
以下是当前输出和所需输出。
list_of_strings = ['This is string one.', 'This is string two.', 'This is string three.'] # current output
list_of_words = ['This', 'is', 'string', 'one', 'This', 'is', 'string', 'two', 'This', 'is', 'string', 'three'] # desired output
答案 0 :(得分:0)
第一个 for 循环一次提取一行。例如:-
Future<void> redirect(String authorizationUrl) async {
if (await canLaunch(authorizationUrl)) {
await launch(authorizationUrl);
}
}
Future<void> listen(String redirectUrl) async {
await getUriLinksStream().listen((Uri uri) async {
if (uri.toString().startsWith(redirectUrl)) {
responseUrl = uri;
}
});
}
然后list_of_strings[0] = 'This is string one';
,这里word = line.split()
通过分隔符=(空格)将行分割成单词
第二个 for 循环将所有拆分的单词附加或添加到 list_of_words 数组中。
split()
答案 1 :(得分:0)
你可以试试这个。
list_of_words = [j.strip('.') for i in list_of_strings for j in i.split()]
答案 2 :(得分:0)
您可以这样尝试(rstrip
来自 .
的字符串而不是 strip
并围绕空格拆分,加入通过 sum
拆分后获得的列表) :
>>> sum([i.rstrip(".").split(" ") for i in list_of_strings], [])
['This', 'is', 'string', 'one', 'This', 'is', 'string', 'two', 'This', 'is', 'string', 'three']