[英]Finding the Length of a Word in a String and Finding How Many Words Have That Length. Without Using Import And NLTK(Python)
I need some help finding the length of a word and how many word have that length with tabular.我需要一些帮助来查找单词的长度以及有多少单词具有表格的长度。 For example, if the sentence is "I will buy a new bike.",
例如,如果句子是“我要买一辆新自行车。”,
The output would be output 将是
Length of Word![]() |
How Many Words In The Text In This Length![]() |
---|---|
1 ![]() |
1 ![]() |
3 ![]() |
2 ![]() |
4 ![]() |
1 ![]() |
If you prefer doing it without any imports at all:如果您更喜欢完全不导入任何内容:
def wordlenghtsgrouper(phrase):
l = [len(w) for w in phrase.replace('.','').replace(',','').split()]
return {i:l.count(i) for i in l}
It returns a dictionary containing the "lengths" and a count of each ocurrence.它返回一个字典,其中包含“长度”和每次出现的计数。
If you don't mind importing, you can use the Counter which is specifically does what you ask for:如果您不介意导入,您可以使用专门执行您要求的计数器:
from collections import Counter
...
def wordlenghtsgrouper(phrase):
return Counter([len(w) for w in phrase.replace('.','').replace(',','').split()])
The code below first gets rid of all punctuation marks, then split the sentence into a list of words, then creates a dictionary of lengths and counts, and finally prints the output in tabular format without importing anything.下面的代码首先去掉所有标点符号,然后将句子拆分成一个单词列表,然后创建一个长度和计数的字典,最后以表格格式打印 output 而不导入任何内容。
sentence = "I will' buy; a new bike."
#remove punctuation marks
punctuations = ['.', ',', ';', ':', '?', '!', '-', '"', "'"]
for p in punctuations:
sentence = sentence.replace(p, "")
#split into list of words
word_list = sentence.split()
#create a dictionary of lengths and counts
dic = {}
for word in word_list:
if len(word) not in dic:
dic[len(word)] = 1
else:
dic[len(word)] += 1
#write the dictionary as a table without importing anything (e.g.Pandas)
print('Length of word | Count of words of that length')
for length, count in dic.items():
print('------------------------------------------')
print(f' {length} | {count}')
#Output:
#Length of word | Count of words of that length
#------------------------------------------
# 1 | 2
#------------------------------------------
# 4 | 2
#------------------------------------------
# 3 | 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.