"Newbie" questions - "unique" sorting ?
Erik Max Francis
max at alcyone.com
Wed Jun 18 20:13:59 EDT 2003
John Fitzsimons wrote:
> (A) Is there a better newsgroup than this one for "newbies" ? If so
> what is it please ?
This group has a pretty good range of capabilities.
> (B) I am wanting to sort words (or is that strings ?) into a list from
> a clipboard and/or file input and/or....
You can just use the string's split method:
>>> 'The quick brown fox jumped over the lazy dog.'.split()
['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy',
'dog.']
This will actually return a list of any non-whitespace strings separated
by whitespace, which isn't going to technically be a "word," since it
will include things like hyphens. If you want finer control, you can
specify a regular expression and use re.findall. What regular
expression you'd want really depends on precisely what criteria are
important to you to identifying "words."
> (C) To sort out the list of "unique" words/strings.
Just sort the list:
>>> 'The quick brown fox jumped over the lazy dog.'.split()
['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy',
'dog.']
>>> words = 'The quick brown fox jumped over the lazy dog.'.split()
>>> words
['The', 'quick', 'brown', 'fox', 'jumped', 'over', 'the', 'lazy',
'dog.']
>>> words.sort()
>>> words
['The', 'brown', 'dog.', 'fox', 'jumped', 'lazy', 'over', 'quick',
'the']
This does an ASCII sorting, so (for instance) capital letters will sort
earlier than lowercase letters. You can specify a custom comparison
function as an argument to the .sort method; for instance, to sort case
insensitively:
>>> words.sort(lambda x, y: cmp(x.lower(), y.lower()))
>>> words
['brown', 'dog.', 'fox', 'jumped', 'lazy', 'over', 'quick', 'The',
'the']
> (D) Perhaps there is a "grep" python script that finds all http, ftp,
> www (unique ?) address' in a text file ?
That would be good for re.findall. You specify the pattern you want via
a regular expression, poke it at a string and it returns a list of the
string matches. That would require learning regular expressions, of
course.
--
Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ My reputation grows with every failure.
\__/ George Bernard Shaw
More information about the Python-list
mailing list