473,473 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Python least characters function

4 New Member
I have to write a function which takes one argument text containing a block of text in the form of a str, and returns an alphabetically sorted list of word(s) with the lowest “vowel proportion” as a list. The vowel proportion is defined as the proportion of letters in the word which are vowels (e.g. the vowel proportion of "life" is 2 = 0.5).
Apr 19 '13 #1
2 1635
bvdet
2,851 Recognized Expert Moderator Specialist
Generally you are on the right track, but I think you have some problems with your algorithm. What you want is to create a dict with the frequencies as keys and words as values. First, I would create a function that returns the number of vowels in a word. Your dictionary values should be lists so your result would include words that have the same vowel proportion. dict method setdefault is ideal for this. Return the value of dict[min(dict)] since that is the answer you are looking for.

Something like:
Expand|Select|Wrap|Line Numbers
  1. ...snip...
  2.     words = sentence.lower().split()
  3.     least_characters = {}
  4.     for word in words:
  5.         least_characters.setdefault(vowel_count(word)/float(len(word)), []).append(word)
  6.     return least_characters[min(least_characters)]

I change some of the identifier names so it would be more intuitive. vowel_count is the function returning the number of vowels in a word. That could be reduced to a one-line list comprehension - possibly:
Expand|Select|Wrap|Line Numbers
  1. len([letter for letter in word if letter in ['a', 'e', 'i', 'o', 'u']])
Apr 19 '13 #2
dwblas
626 Recognized Expert Contributor
You punc_char substitution is bass-ackwards in that an "&" will be included because it is not in your list. To add to bvdet's post, and this is a little too much nesting for my tastes so a function(s) should be included, perhaps to process each word individually, to reduce the nesting and improve readability.
Expand|Select|Wrap|Line Numbers
  1.     vowel = ['a','e','i','o','u']
  2.     words = sentence.lower().split()
  3.     least_characters = {}
  4.     for word in words:
  5.         for ltr in word:
  6.             if "a" <= ltr <= "z":  # omits everything else
  7.                 if ltr in vowel: 
Apr 19 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

40
by: Xah Lee | last post by:
is it possible in Python to create a function that maintains a variable value? something like this: globe=0; def myFun(): globe=globe+1 return globe
0
by: PixelDust1 | last post by:
Hi guys, I am pretty new to Python and to embedding it in C. Here is what I am trying to accomplish: I have a C program that accepts user input, which can be a Python script which includes...
7
by: Chris Jones | last post by:
I'm trying to make sense of a python program and was wondering if vim has any python-oriented functionalities (apart from syntax highlighting) that would make it somewhat easier to browse the...
3
by: Allan | last post by:
Hi All - We're developing in Python 2.4.3 and are noticing something strange. For example, when testing, here's what we're seeing: x = "here's my title!"; x = x.title(); print x; Here'S My...
5
by: John L. Whelan | last post by:
Hi Everyone: I know that I can repeat a single character using the StrDup function. Is there a function that will allow me to repeat a string? StrDup(5,"John") returns "JJJJJ." Is there a...
1
by: Wijaya Edward | last post by:
Hi, How does one benchmark multiple subroutines in Python? Is there a built-in library for that? -- Regards, Edward WIJAYA SINGAPORE
0
by: William Manley | last post by:
Hello. I was wondering if any of you have ever done this before or could point me in the right direction. sub test() hWnd = API_FindWindow("ConsoleWindowClass", "Python") SendCommand...
1
by: frankrentef | last post by:
In using PAMIE 2.0 specifically the "scripWrite" function I'm getting the following error... File "C:\Python23\Lib\site-packages\win32com\client\dynamic.py", line 489, in _getattr_ further...
3
by: prelude | last post by:
I'm looking into how the Python's pack function is implemented. Does anyone know where the source is located? I grep the entire lib folder but couldn't find it. I have the unfortunate task of...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.