我正在处理一段话。我需要按字母顺序对段落中的单词进行排序,然后按反向频率对它们进行排序。当我的单词计数功能对通道进行排序时,它也计算空白空间。我做了一些修改,它仍然计算空字符串。我想知道是否还有其他方法可以做到这一点。我的代码是:
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 )
答案 0 :(得分:0)
这应该让你朝着正确的方向前进,你需要处理它,可能是通过剥去句号和冒号,你可能想要将它全部小写。
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)
答案 1 :(得分:0)
要从字符串列表中过滤空字符串,我会使用:
my_list = filter(None, my_list)
答案 2 :(得分:0)
也许您正在寻找str.isspace()
答案 3 :(得分:0)
而不是:
if word!='':
你应该使用:
if word.strip()!='':
因为第一个检查零长度字符串,并且您想要消除长度不为零的空格。剥离一个空格字符串将使其为零长度。