关于消除字数中的空白空间
我正在处理一段文字。我需要把这段文字里的单词按字母顺序排列,然后再按出现频率的倒序排列。可是我发现我的单词计数功能在排序的时候也把空格算进去了。我做了一些修改,但它还是会把空字符串算上。我在想有没有其他方法可以做到这一点。我的代码是:
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 )
4 个回答
0
要从一个字符串列表中筛选出空字符串,我会使用:
my_list = filter(None, my_list)
0
也许你在找的是 str.isspace() 这个方法。
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)
0
不要使用:
if word!='':
你应该使用:
if word.strip()!='':
因为第一个方法是检查字符串是否为空,而你其实想要去掉那些不是空的字符串中的空格。把只有空格的字符串去掉空格后,就变成了空字符串。