【问题标题】:How to extract words from a list of lists and filter words by length?如何从列表列表中提取单词并按长度过滤单词?
【发布时间】:2019-06-14 02:34:47
【问题描述】:

基本上我想用 python 做两件事: 1) 使结果列表成为单词列表,而不是列表列表,以及 2) 过滤掉长度为 1 个字符的单词。


我必须从字典列表中提取单词,将单词设为小写,然后过滤单词,以便只有长度大于 1 的单词才是结果列表的一部分。我必须使用 map() 和列表理解,但我也不知道该怎么做。我还需要使用 re.spilt() 来拆分单词并去掉不需要的标点符号。

到目前为止,我已经能够提取字典列表的相关部分,将单词拆分,并将所有单词变成小写。但我得到的是一个列表,其元素是单词。

我希望结果只是一个长度为 2 个或更多字符的单词列表。

def extract_tweets(some_list):
    tweetlist = []
    for each_tweet in some_list:
        text = each_tweet['text']
        lowercase = text.lower()
        tweetlist.append(lowercase)
    tweetwords = []
    for words in tweetlist:
        word = re.split('\W+', words)
        tweetwords.append(word)
    return(tweetwords)

【问题讨论】:

  • 如果您想要一个字符串列表而不是列表列表,请将最后一个 tweetwords.append(word) 更改为 tweetwords.extend(word),因为 re.split 返回一个列表,而不是单个字符串。

标签: python list list-comprehension text-extraction data-extraction


【解决方案1】:

一个简单的列表理解将帮助您:

tweetwords = [word for word in tweetwords if len(word) > 1]

【讨论】:

    【解决方案2】:

    要工作,您的函数extract_tweets 需要一个字典列表作为参数。所以 some_list 看起来像这样:

    some_list = [
        {
            'text': "Hello world!"
        },
        {
            'text': "The sun is shinning, the sky is blue."
        },
    ]
    

    实际上,第一个循环提取文本,因此最好将其称为 textstext_list(而不是 tweetlist)。你得到:

    ['hello world!', 'the sun is shinning, the sky is blue.']
    

    要提取文本的单词,最好使用findall 而不是split,因为split 如果文本以非单词开头或结尾,则可以有空字符串,例如我的例子。

    要查找文本的所有单词,您可以使用:

    words = re.findall('\w+', text)
    

    注意:\w+ RegEx 还将捕获数字和下划线。为了避免这种情况,你应该使用否定类[^\W\d_]+

    findall 的结果是一个单词列表。要过滤长度大于 1 的单词,您可以将 filter 与函数或带有条件的理解列表一起使用:

    words = list(filter(lambda w: len(w) > 1, words))
    # or:
    words = [w for w in words if len(w) > 1]
    

    这是重构后的代码:

    import re
    import pprint
    
    
    def extract_tweets(some_list):
        texts = []
        for each_tweet in some_list:
            text = each_tweet['text']
            lowercase = text.lower()
            texts.append(lowercase)
        tweet_words = []
        for text in texts:
            words = re.findall('[^\W\d_]+', text)
            words = [w for w in words if len(w) > 1]
            tweet_words.append(words)
        return tweet_words
    

    下面的例子……

    some_list = [
        {
            'text': "Hello world!"
        },
        {
            'text': "The sun is shinning, the sky is blue."
        },
        {
            'text': "1, 2, 3, four"
        },
        {
            'text': "not a word"
        },
    ]
    
    pprint.pprint(extract_tweets(some_list))
    

    ……你得到:

    [['hello', 'world'],
     ['the', 'sun', 'is', 'shinning', 'the', 'sky', 'is', 'blue'],
     ['four'],
     ['not', 'word']]
    

    使用extend 而不是append,您会得到:

    ['hello',
     'world',
     'the',
     'sun',
     'is',
     'shinning',
     'the',
     'sky',
     'is',
     'blue',
     'four',
     'not',
     'word']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 2019-04-28
      • 1970-01-01
      • 2022-08-17
      • 2021-07-17
      相关资源
      最近更新 更多