The longest word

N

NiklasRTZ

Newbie hello onwards to the .py manual asks meantime how the longest
word gets determined?
First word, ok
'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]
'a'
rfind is another subset
'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)]
'a aa aaa'
One row should be able. It's a direct word 'a aa aaa...' and can be a
variable, it's no big deal it occurs twice still naturally easier if
also occured just once.
Sincere thanks & regards
Niklas
 
B

Bearophile

Newbie hello onwards to the .py manual asks meantime how the longest
word gets determined?
First word, ok
 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]

Your language is not easy to understand, but I think you want to find
the longest word in a string.
If this is the input string:
txt = "a aa aaa aa"

There are several ways to do it, I'll show a simple one.

You can split it into its parts (not having Python a built-in lazy
split yet, you can split it all at once). You can do it with the
string split method. It produces a list of the words, more or less
(but you may have words like "gone,", you may have to take care of
them too, this requires some code.

Once you have a list of words, you have to take the longest. A simple
way is to replace each word with a tuple that contains the length of
the word and the word itself, then pick the tuple with the highest
length value. But Python allows you to do such operation in a faster
way: you can use the max() function, it has a "key" function that
allows you to remap on the fly what you mean by "max". So you just
have to give it a function (reference) that given the word spits its
length, such function is "len" itself.

If you instead want to find the position of the longest word the
program gets a little longer. Ask if you need something different.

Bye,
bearophile
 
N

NiklasRTZ

Newbie hello onwards to the .py manual asks meantime how the longest
word gets determined?
First word, ok
 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]

Your language is not easy to understand, but I think you want to find
the longest word in a string.
If this is the input string:
txt = "a aa aaa aa"

There are several ways to do it, I'll show a simple one.

You can split it into its parts (not having Python a built-in lazy
split yet, you can split it all at once). You can do it with the
string split method. It produces a list of the words, more or less
(but you may have words like "gone,", you may have to take care of
them too, this requires some code.

Once you have a list of words, you have to take the longest. A simple
way is to replace each word with a tuple that contains the length of
the word and the word itself, then pick the tuple with the highest
length value. But Python allows you to do such operation in a faster
way: you can use the max() function, it has a "key" function that
allows you to remap on the fly what you mean by "max". So you just
have to give it a function (reference) that given the word spits its
length, such function is "len" itself.

If you instead want to find the position of the longest word the
program gets a little longer. Ask if you need something different.

Bye,
bearophile

Thank you. This seems to work:
sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
len(b))[-1]
'sdfsdfsdfsdf'
 
P

Piet van Oostrum

NiklasRTZ said:
N> Newbie hello onwards to the .py manual asks meantime how the longest
N> word gets determined?
N> First word, ok
N> 'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]
N> 'a'
N> rfind is another subset
'a aa aaa aa'[:'a aa aaa aa'.rfind(' ',1,10)]
N> 'a aa aaa'
N> One row should be able. It's a direct word 'a aa aaa...' and can be a
N> variable, it's no big deal it occurs twice still naturally easier if
N> also occured just once.
N> Sincere thanks & regards
N> Niklas

1. Is this homework?
2. It sounds very confusing. Are you trying to find the longest word in
a series of words seperated by spaces? find and rfind will not help
you much. You should start with var.split() if var contains the text.
 
P

Piet van Oostrum

NiklasRTZ said:
N> Thank you. This seems to work:
N> sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
N> len(b))[-1]
N> 'sdfsdfsdfsdf'

simpler:

sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len)[-1]
 
P

Peter Otten

NiklasRTZ said:
Newbie hello onwards to the .py manual asks meantime how the longest
word gets determined?
First word, ok
'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]

Your language is not easy to understand, but I think you want to find
the longest word in a string.
If this is the input string:
txt = "a aa aaa aa"

There are several ways to do it, I'll show a simple one.

You can split it into its parts (not having Python a built-in lazy
split yet, you can split it all at once). You can do it with the
string split method. It produces a list of the words, more or less
(but you may have words like "gone,", you may have to take care of
them too, this requires some code.

Once you have a list of words, you have to take the longest. A simple
way is to replace each word with a tuple that contains the length of
the word and the word itself, then pick the tuple with the highest
length value. But Python allows you to do such operation in a faster
way: you can use the max() function, it has a "key" function that
allows you to remap on the fly what you mean by "max". So you just
have to give it a function (reference) that given the word spits its
length, such function is "len" itself.

If you instead want to find the position of the longest word the
program gets a little longer. Ask if you need something different.

Bye,
bearophile

Thank you. This seems to work:
sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
len(b))[-1]
'sdfsdfsdfsdf'

To spell out bearophiles more efficient suggestion:
'sdfsdfsdfsdf'
 
P

Peter Otten

NiklasRTZ said:
Newbie hello onwards to the .py manual asks meantime how the longest
word gets determined?
First word, ok
'a aa aaa aa'[:'a aa aaa aa'.find(' ',1,10)]

Your language is not easy to understand, but I think you want to find
the longest word in a string.
If this is the input string:
txt = "a aa aaa aa"

There are several ways to do it, I'll show a simple one.

You can split it into its parts (not having Python a built-in lazy
split yet, you can split it all at once). You can do it with the
string split method. It produces a list of the words, more or less
(but you may have words like "gone,", you may have to take care of
them too, this requires some code.

Once you have a list of words, you have to take the longest. A simple
way is to replace each word with a tuple that contains the length of
the word and the word itself, then pick the tuple with the highest
length value. But Python allows you to do such operation in a faster
way: you can use the max() function, it has a "key" function that
allows you to remap on the fly what you mean by "max". So you just
have to give it a function (reference) that given the word spits its
length, such function is "len" itself.

If you instead want to find the position of the longest word the
program gets a little longer. Ask if you need something different.

Bye,
bearophile

Thank you. This seems to work:
sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
len(b))[-1]
'sdfsdfsdfsdf'

To spell out bearophile's more efficient suggestion:
'sdfsdfsdfsdf'
 
N

NiklasRTZ

There were 3 solutions. Everybody's homework is someone's "what's
this?"
max('asdasfd faop29v8 asejrhjhw awg5w35g aw3g5aw3g
kajhsdkfhlskjdhfakljsdhf awg3 aw3'.split(), key=len)
sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),key=len)[-1]
sorted("a AAA aa aaaaa vv".split(' '),lambda a,b: len(a)-len(b))[-1]
 
H

Hrvoje Niksic

Piet van Oostrum said:
N> Thank you. This seems to work:
N> sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
N> len(b))[-1]
N> 'sdfsdfsdfsdf'

simpler:

sorted("a AAA aa aaaaa sdfsdfsdfsdf vv".split(' '), key=len)[-1]

There is no need to sort the sequence to obtain the largest element.
The max function is designed to do exactly that, and also supports the
key argument:
'sdfsdfsdfsdf'
 
N

NiklasRTZ

Piet van Oostrum said:
N> Thank you. This seems to work:
N> sorted("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '),lambda a,b: len(a)-
N> len(b))[-1]
N> 'sdfsdfsdfsdf'

sorted("a AAA aa aaaaa  sdfsdfsdfsdf vv".split(' '), key=len)[-1]

There is no need to sort the sequence to obtain the largest element.
The max function is designed to do exactly that, and also supports the
key argument:

'sdfsdfsdfsdf'

Sincere thanks for strengthening python's superior flexibility. Same
function also works around an exploding index problem returning
results for longest word where otherwise a word with whitespace
crashes the index:
all().search(max(q.split(), key=len)).filter("modified >",
timeline).filter("published =", True).filter("modified <=",
bookmark ).order("-modified").fetch(PAGESIZE+1)
Line below crashes the index for words with whitespace (unknown
researchable, only occurs live, works with development)
all().search(q).filter("modified >", timeline).filter("published =",
True).filter("modified <=", bookmark ).order("-modified").fetch
(PAGESIZE+1)
best regards,
Niklas
 
R

ryles

Sincere thanks for strengthening python's superior flexibility. Same
function also works around an exploding index problem returning
results for longest word where otherwise a word with whitespace
crashes the index:

Babelfish?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,653
Latest member
YvonneJif

Latest Threads

Top