本文介绍了字母频率计数器/你的想法..的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


这是我的字母频率计数器代码。这对我来说似乎很臃肿,而且对于什么是更好的方式的任何建议(请记住我的想法)我将非常感激...


def valsort(x):

res = []

表示密钥,x.items()中的值:

res.append((价值,关键))

返回res


def mostfreq(strng):

dic = {}

用于strng中的字母:

如果字母不在dic中:

dic.setdefault(letter,1)

else:

dic [letter] + = 1

newd = dic.items()

getvals = valsort( newd)

getvals.sort()

length = len(getvals)

返回getvals [长度 - 3:长度]


非常感谢!!

解决方案



我觉得初学者不错:)



略短:

def mostfreq(strng):

dic = {}

为strng中的字母:

如果字母不在dic中:

dic [letter] = 0

dic [字母] + = 1

#交换字母,在此计算,因为我们要先按计数排序

getvals = [(pair [1],pair [0])for配对在dic.iteritems()]

getvals.sort()

返回getvals [-3:]


我'我不确定你是否希望函数mostfreq能够返回前3个字母的3个最常见的
频繁字母?它似乎做了后者。上面的

代码使用前者,即频率最高的字母。


Paul




我觉得初学者不错:)




略短:


def mostfreq(strng):

* * * dic = {}

* * *用于strng中的字母:

* * * * *如果字母不在dic中:

* * * * * * * dic [letter] = 0

* * * * * dic [letter] + = 1

* * *#交换字母,在此计算我们要排序在第一次计数

* * * getvals = [(pair [1],pair [0])对于dic.iteritems()中的一对

* * * getvals。 sort()

* * *返回getvals [-3:]


我不确定*你是否想要函数mostfreq返回3最多

前3个字母的常用字母?它似乎做了后者。上面的

代码使用前者,即频率最高的字母。


Paul-隐藏引用文字 -


- 显示引用的文本 -



我想我会尝试在磁盘上获得双端队列。持续索引。在b-tree中存储
磁盘地址。 不到需要多长时间?是一个

扇区,里面有什么?




我不会对算法发表评论,但我认为你应该尝试为你的变量找到更好的名字。在上面的代码片段中你有x,

res,dic,newd,length,getvals,这些都没有给出很多线索,因为

它们用于什么。


例如


* dic = {}

我们知道这是一个字典,但是一个字典什么?


* newd = dic.items()

听起来像''新词典'',但显然不是它的列表

的关键值组。


* length = len(getvals)

再次,我们知道这是一个长度,但是什么的长度?


HTH


-

Arnaud

Hello,

Here is my code for a letter frequency counter. It seems bloated to
me and any suggestions of what would be a better way (keep in my mind
I''m a beginner) would be greatly appreciated..

def valsort(x):
res = []
for key, value in x.items():
res.append((value, key))
return res

def mostfreq(strng):
dic = {}
for letter in strng:
if letter not in dic:
dic.setdefault(letter, 1)
else:
dic[letter] += 1
newd = dic.items()
getvals = valsort(newd)
getvals.sort()
length = len(getvals)
return getvals[length - 3 : length]

thanks much!!

解决方案

Not bad for a beginner I think :)

Slightly shorter:

def mostfreq(strng):
dic = {}
for letter in strng:
if letter not in dic:
dic[letter] = 0
dic[letter] += 1
# Swap letter, count here as we want to sort on count first
getvals = [(pair[1],pair[0]) for pair in dic.iteritems()]
getvals.sort()
return getvals[-3:]

I''m not sure if you wanted the function mostfreq to return the 3 most
frequent letters of the first 3 letters? It seems to do the latter. The
code above uses the former, i.e. letters with highest frequency.

Paul



Not bad for a beginner I think :)



Slightly shorter:

def mostfreq(strng):
* * *dic = {}
* * *for letter in strng:
* * * * *if letter not in dic:
* * * * * * *dic[letter] = 0
* * * * *dic[letter] += 1
* * *# Swap letter, count here as we want to sort on count first
* * *getvals = [(pair[1],pair[0]) for pair in dic.iteritems()]
* * *getvals.sort()
* * *return getvals[-3:]

I''m not sure if *you wanted the function mostfreq to return the 3 most
frequent letters of the first 3 letters? It seems to do the latter. The
code above uses the former, i.e. letters with highest frequency.

Paul- Hide quoted text -

- Show quoted text -

I think I''d try to get a deque on disk. Constant indexing. Store
disk addresses in b-trees. How long does ''less than'' take? Is a
sector small, and what''s inside?


I won''t comment on the algorithm, but I think you should try to find
better names for your variables. In the snippet above you have x,
res, dic, newd, length, getvals which don''t give much of a clue as to
what they are used for.

e.g.

* dic = {}
We know it''s a dict, but a dict of what?

* newd = dic.items()
Sounds like ''new dictionary'', but obviously isn''tas it is a list
of key,value pairs.

* length = len(getvals)
Again, we know it''s a length, but the length of what?

HTH

--
Arnaud


这篇关于字母频率计数器/你的想法..的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 09:44