473,657 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

letter frequency counter / your thoughts..

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((val ue, 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!!
Jun 27 '08 #1
13 3728
um******@gmail. com wrote:
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..
Not bad for a beginner I think :)
def valsort(x):
res = []
for key, value in x.items():
res.append((val ue, 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!!
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
Jun 27 '08 #2
On May 7, 12:30*pm, Paul Melis <p...@floorba ll-flamingos.nlwro te:
umpsu...@gmail. com wrote:
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..

Not bad for a beginner I think :)


def valsort(x):
* *res = []
* *for key, value in x.items():
* * * * * *res.append((va lue, 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!!

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?
Jun 27 '08 #3
um******@gmail. com writes:
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((val ue, 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!!
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
Jun 27 '08 #4
On Wed, May 7, 2008 at 11:30 AM, Paul Melis <pa**@floorba ll-flamingos.nlwro te:
dic = {}
for letter in strng:
if letter not in dic:
dic[letter] = 0
dic[letter] += 1
As a further refinement, you could use the defaultdict class from the
collections module:

dic = defaultdict(int )
for letter in strng:
dic[letter] += 1
Jun 27 '08 #5
On May 7, 1:31*pm, "Ian Kelly" <ian.g.ke...@gm ail.comwrote:
On Wed, May 7, 2008 at 11:30 AM, Paul Melis <p...@floorba ll-flamingos.nlwro te:
* * dic = {}
* * for letter in strng:
* * * * if letter not in dic:
* * * * * * dic[letter] = 0
* * * * dic[letter] += 1

As a further refinement, you could use the defaultdict class from the
collections module:

* *dic = defaultdict(int )
* * * *for letter in strng:
* * * * * *dic[letter] += 1
Sounds like novel flow of control.
Jun 27 '08 #6
That's a great suggestion Arnaud. I'll keep that in mind next time I
post code. Thanks ;)
On May 7, 12:27 pm, Arnaud Delobelle <arno...@google mail.comwrote:
umpsu...@gmail. com writes:
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((val ue, 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!!

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
Jun 27 '08 #7
On May 8, 6:00 am, umpsu...@gmail. com wrote:
That's a great suggestion Arnaud. I'll keep that in mind next time I
post code. Thanks ;)
It's a suggestion for YOUR benefit, not ours. Consider keeping it in
mind next time you WRITE code, whether you intend publishing it or
not.

Jun 27 '08 #8
On 7 mai, 23:50, Paul Rubin <http://phr...@NOSPAM.i nvalidwrote:
(snip)
Someone else suggested the heapq module, which is a good approach
though it might be considered a little bit high-tech. If you
want to use sorting (conceptually simpler), you could use the
sorted function instead of the in-place sorting function:

# return the second element of a 2-tuple. Note how we
# use tuple unpacking: this is really a function of one argument
# (the tuple) but we specify the arg as (a,b) so the tuple
# is automatically unpacked on entry to the function.
# this is a limited form of the "pattern matching" found in
# languages like ML.
def snd((a,b)): return b
operator.itemge tter does this already
return sorted(dic.iter items, key=snd, reverse=True)[-3:]
you want to actually call iteritems here !-)

return sorted(dic.iter items(), key=operator.it emgetter(1),
reverse=True)

Thanks for reminding me the 'key' argument to sorted anyway - I too
often tend to forget it.
Jun 27 '08 #9
On 7 mai, 23:51, "bruno.desthuil li...@gmail.com "
<bruno.desthuil li...@gmail.com wrote:
(snip)

Small improvement thanks to Paul Rubin:

from collections import defaultdict
from operator import itemgetter

def get_letters_fre quency(source):
letters_count = defaultdict(int )
for letter in source:
letters_count[letter] += 1
return sorted(
letters_count.i teritems(),
key=itemgetter( 1),
reverse=True
)

Jun 27 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
9672
by: Dung Ping | last post by:
Such as: <script> //code aaaa zzzz ccc
8
6334
by: Pete | last post by:
I need to write a program to do a frequency analysis on a string of text. Firstly I need to get the alphabet A to Z for comparison but I'm not sure how to go about this. Is the following the only way to do it: Alphabet = {'a','b','c',...,'z'}; or is there another way to represent the alphabet characters. Thankyou
19
3136
by: jason_box | last post by:
I'm alittle new at C and I'm trying to write a simple program that will record the frequency of words and just print it out. It is suppose to take stdin and I heard it's only a few lines but I'm not sure where to start. The only example I have to work with is if you ran the program say: File list contains: This is a test.
2
2197
by: gdarian216 | last post by:
the program reads input from a file and then outputs the averages and grade. for some reason it is reading in the same line twice and it doesn't print out the grade. everything else is correct, if someone can help me thanks this is my code.... #include <cstdlib> #include <iostream> #include <string> #include <vector>
2
2027
by: majorecono | last post by:
I took a look at the site and found this as a counter. Is there any way I can use it in a dialog box, that asks user to input any number of letters in a (?)dialog box, pressing enter and out comes Number of upper/lower case e's in the (I)dialog box. Also have it repeat process until user enters Stop. Well yeah, thanks if anyone can help put that up tonight.. String str = "Count" ; char ch = 'e'; char ch2 = 'E'; int count = 0 ;
3
5054
by: majorecono | last post by:
What I'm bad at is the dialog box part. This is the code Ive come up with... if you see a way to improve what im thinkng of.... post it please. Program should.... 1. Ask user to type in a sentence, using JOptionPane.showInputDialog(). 2. The program will scan each letter in the string and count how many times the upper case letter 'E' appears, and how many times the lower case letter 'e' appears. 3. Using a JOptionPane.showInputDialog(),...
5
4536
by: svd | last post by:
so i'm attempting to make a program that reads in a file and then counts the frequency of each letter of the alphabet in the file and then outputs the letter with the frequency into a different file.. i didn't include the first part of the program because i'm pretty sure it's correct... i really have no clue what's wrong with this so any help would be great vector<int> occurrencesVec(26); int i; for(;;) { fin >> i; if...
5
6924
by: Larry | last post by:
Dear all, I'm new to Python. I have a file (an image file actually) that I need to read pixel by pixel. It's an 8-bit integer type. I need to get the statistics like mean, standard deviation, etc., which I know a little bit already from reading numpy module. What I want to know is how to get the number of occurences of numeric element in an array. Say, b = array(()
3
2607
by: Bob Alston | last post by:
I have a system built using Access replication. After some initial problems dealing with memo fields, it now seems to be working well. I am curious as to how frequently others Compress & Repair replicated databases? Bob
0
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8732
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7327
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.