我已经编写了一个适用于长度为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
不太欢迎使用import:/
答案 0 :(得分:-1)
您可以创建一个生成器而不是遍历长度为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)]
答案 1 :(得分:-1)
三个步骤:
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]
然后计算此列表中每个数字的数量。 这会让你感动吗?