字符串列表到单词列表

时间:2021-01-12 11:01:35

标签: python-3.x string list data-science

所以,我试图找出获取字符串列表并将其转换为单词列表的最佳方法。我还想从字符串中删除所有标点符号。我的思考过程是:

  1. 使用 .join() 方法和列表理解/映射制作一个大字符串列表。
  2. 使用字符串翻译方法去除标点符号。
  3. 使用 split 方法将巨大的字符串拆分回一个列表。

从字符串列表到单词列表,这似乎需要很多步骤。有没有人有更简洁的方法或可以对我的过程提出建议?最终目标是将字符串列表传递给计数器类以找到最常见的单词。

以下是当前输出和所需输出。

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

3 个答案:

答案 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']