best way to align words?
Mitja Trampus
nun at example.com
Thu Nov 30 18:07:27 EST 2006
Robert R. wrote:
> Hello,
>
> i would like to write a piece of code to help me to align some sequence
> of words and suggest me the ordered common subwords of them
>
> a trouble i have if when having many different strings my results tend
> to be nothing while i still would like to have one of the, or maybe,
> all the best matches.
"align"?
Anyway, for finding the commonest words, you'll be best off
counting how many times each word appears:
lst = ["foo bar baz", "qux foo foo kaka", "one foo and kaka
times qux"]
for line in lst:
for word in line.split():
count[word] = count.get(word,0) + 1
Now you go for the ones with the highest count:
for (word, n) in sorted(d.items(), key = lambda x: x[1],
reverse = True):
print word, 'appears', n, 'times'
Untested. If you want to count the number of lines a word
appears in (as opposed to the number of times it appears at
all), add an extra condition before count[word] = ...
More information about the Python-list
mailing list