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

Home Posts Topics Members FAQ

Alphabetizing?

Ok, im trying to creat a small simple program that alphabetizes user
inputed words. I thought why not just create a def that splits all the
words down into char's then take the first char and crosscheck it with all
lowercase letters. Then find its position and return the results to the
user in order or 1-26! But this takes a long time and the code gets
confusing. Any suggestion? Thanx in advance!
When you see the net take the shot
When you miss your shot shoot again
When you make your shot you win!

Just remember
Offense sells tickets
But defense wins championships!

Jul 18 '05 #1
2 7041
In article <ma************ *************** *******@python. org> wrote:
Ok, im trying to creat a small simple program that alphabetizes user
inputed words. I thought why not just create a def that splits all the
words down into char's then take the first char and crosscheck it with all
lowercase letters. Then find its position and return the results to the
user in order or 1-26! But this takes a long time and the code gets
confusing. Any suggestion? Thanx in advance!

How about just sort()ing them?

You can define a custom sort function if you want an
alphabetical rather than lexical sort. ie:
words = []
while 1:
word = raw_input('ente r word ("done" to finish) :')
if word == 'done':
break
words.append(wo rd)

def sort_alpha(a, b):
if a.lower() < b.lower():
return -1
elif a.lower() > b.lower():
return 1
else:
return 0

words.sort(sort _alpha)

print words
Jul 18 '05 #2
Lee Harr wrote:
How about just sort()ing them?

You can define a custom sort function if you want an
alphabetical rather than lexical sort. ie: ... def sort_alpha(a, b):
if a.lower() < b.lower():
return -1
elif a.lower() > b.lower():
return 1
else:
return 0

words.sort(sort _alpha)


You've got the right approach (sorting based on a signature derived from
the string, rather than the string itself), although to be picky this
isn't yet an alphabetic sort, since it should be stripping away
non-alphabetic characters:
sort_alpha("whe re's", "whereas")

-1

Of course this starts to get into questions of what you consider a word
or not, but a true alphabetic sort would remove non-alphabetic
characters first. You can do this easily enough with string.maketran s
and S.translate.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ An undevout astronomer is mad.
\__/ Edward Young
Jul 18 '05 #3

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

Similar topics

7
2236
by: Matthew Wilson | last post by:
Hi- I'm writing a bunch of classes, several of which need functions and variables defined in the math module. In some instances, I'm going to import my module like this: import myshapes and then in others, I'll do:
2
2124
by: Simon Morgan | last post by:
I hope this isn't OT, I looked for a newsgroup dealing purely with algorithms but none were to be found and seeing as I'm trying to implement this in C I thought this would be the best place. I have an array of structs containing data which I'd like to output ordered based on the value of a single member. I was wondering if there is a relatively simple way of doing this without actually modifying the structure of the array? I had a...
4
1829
by: gener672 | last post by:
Searched forum but didn't see. My CSS file is getting large & it's difficult to find things .... is there a program for alphabetizing?
10
1713
Stang02GT
by: Stang02GT | last post by:
Hello, I've been placed on a web analytics project to try and get so me Java experience/web development. I have very little Java knowledge, and hoping that you guys can help me out a little. I have been asked to come up with a Java method that takes a URL string and alphabetizes the parameters. This is what i was given to start... int questionPos = inUrl.indexOf("?"); if (questionPos >= 0) { StringBuffer sb...
1
1198
by: =?Utf-8?B?VEs=?= | last post by:
I have a list of underlined words. they are numbered and have the definitions next to them. I would like to be able to alphabetize them ascending. How do I do this? Please email me at toms123@netzero.com
0
8397
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
8310
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
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
7333
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
6167
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...
0
5632
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4315
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1957
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1620
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.