计算字符串中子字符串出现的百分比,python
我已经编写了一个函数,它适用于长度为1的字符串,但我不知道如何使它适用于更长的字符串计算字符串中子字符串出现的百分比,python,python,string,python-3.x,Python,String,Python 3.x,我已经编写了一个函数,它适用于长度为1的字符串,但我不知道如何使它适用于更长的字符串 def function(text, n): dict={} char_count=0 for c in text: keys=dict.keys() if c.isalpha()==True: char_count+=1 if c in keys: dict[c] +=1
def function(text, n):
dict={}
char_count=0
for c in text:
keys=dict.keys()
if c.isalpha()==True:
char_count+=1
if c in keys:
dict[c] +=1
else:
dict[c]=1
for key in dict:
dict[key]=dict[key]/char_count
return dict
导入的使用不太受欢迎:/您可以创建一个生成器,然后在长度
n
的每个子字符串上迭代。然后将它们用作跟踪计数的字典的键
def substring_percentage(text, n):
out = {}
n_substrings = len(text)-n+1
subs = (text[i:i+n] for i in range(n_substrings))
for s in subs:
if s in out:
out[s] += 100 / n_substrings
else:
out[s] = 100 / n_substrings
return out
测试:
s = 'I have an assignment to write a function that will receive a sentence and a number ' \
+'and will return the percentage of the occurrences of strings of length of the given ' \
+'number in the given string.'
pcts = substring_percentage(s, 4)
sorted(pcts.items(), key=lambda x: x[::-1], reverse=True)
# returns:
[('the ', 2.094240837696335),
(' the', 2.094240837696335),
(' of ', 2.094240837696335),
('n th', 1.5706806282722514),
...
(' an ', 0.5235602094240838),
(' a s', 0.5235602094240838),
(' a n', 0.5235602094240838),
(' a f', 0.5235602094240838)]
三个步骤:
- 将输入拆分为单个单词;Python的
函数将为您返回一个很好的列表split
- 列出相应的字长;在每个元素上使用
len
- 使用
功能计算每个长度的出现次数;把这些结果放在字典里count
sentence = "Now I will a rhyme construct " + \
"By chosen words the young instruct " + \
"Cunningly ensured endeavour " + \
"Con it and remember ever " + \
"Widths of circle here you see " + \
"Stretchd out in strange obscurity "
把这个分成几个单词。列出每个单词的长度;它看起来是这样的:
[3, 1, 4, 1, 5, 9, 2, 6,
5, 3, 5, 8, 9, 7, 9, 3,
2, 3, 8, 4, 6, 2, 6, 4,
3, 3, 8, 3, 2, 7, 9]
然后你计算每个数字中有多少在这个列表中。
这会让你动起来吗?提示:首先使用拆分字符串,然后计算结果列表中不同大小元素的数量。似乎有人对每个答案投了反对票;请解释一下?但是我如何使用您的解决方案使函数不计算空格?例如在删除空格时,
“我是一顶帽子”->“iamahat”
?非常感谢!我只是不想让函数计算包含空格的子字符串,所以在添加到字典之前,我添加了一个if:)