当前位置: 代码迷 >> python >> 关于消除字数统计中的空白
  详细解决方案

关于消除字数统计中的空白

热度:71   发布时间:2023-06-27 21:24:16.0

我正在处理一段。 我需要按字母顺序对文章中的单词进行排序,然后按相反的频率对其进行排序。 当我的单词计数功能对段落进行排序时,它也会对空白区域进行计数。 我做了一些修改,它仍然计算空字符串。 我想知道是否还有其他方法可以做到。 我的代码是:

def build_map( in_file, word_map ):

    for line in in_file:

        # Splits each line at blank space and turns it into
        # a list.
        word_list = line.split()

        for word in word_list:
            if word!='':

                # Within the word_list, we are stripping empty space
                # on both sides of each word and also stripping any
                # punctuation on both side of each word in the list.
                # Then, it turns each word to the lower case to avoid
                # counting 'THE' and 'the' as two different words.
                word = word.strip().strip(string.punctuation).lower()#program revised
                add_word( word_map, word )

这应该使您朝着正确的方向前进,您可能需要通过删除句点和冒号来对其进行处理,并且无论如何都希望使其全部变为小写。

passage = '''I am dealing with a passage. I am required to sort the words in the passage alphabetically and then sort them by reverse frequency. When my word count function sorts the passage, it counts empty space too. I did some modification and it still counts the empty spaces. I am wondering if there is any other way to do it. My codes are:'''

words = set(passage.split())

alpha_sort = sorted(words, key=str.lower)

frequency_sort = sorted(words, key=passage.count, reverse=True)

要从字符串列表中过滤空字符串,我将使用:

my_list = filter(None, my_list)

也许您正在寻找

代替:

if word!='':

您应该使用:

if word.strip()!='':

因为第一个检查零长度的字符串,并且您要消除不是零长度的空格。 剥离仅空格字符串将使其长度为零。