Home > Software engineering >  How to find longest string from a string separated by comma in python
How to find longest string from a string separated by comma in python

Time:10-17

I have a string separated by commas ,. I want to find the longest string from the given string.

words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'

What I did so far

print(max(words.split(','), key=len))

And I am getting this output abcdefghi but as you can see abcdefghi and barracuda have same length. So, why I am only getting one instead of two or all.

Also

words = 'fishes,sam,gollum,sauron,frodo,balrog'

in the above string many words have same length. I want to return every one of them.

CodePudding user response:

You can zip len of word to word then create dict from len and return largest len like below:

>>> from collections import defaultdict
>>> words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
>>> dct = defaultdict(list)
>>> lstWrdSplt = words.split(',')
>>> for word, length in (zip(lstWrdSplt,(map(len,lstWrdSplt)))):
...    dct[length].append(word)
    
>>> dct[max(dct)]
['abcdefghi', 'barracuda']

# for more explanation
>>> dct 
defaultdict(list,
            {3: ['run'],
             4: ['barn', 'fish', 'swim'],
             9: ['abcdefghi', 'barracuda'],
             6: ['yellow'],
             5: ['shark']})

You can use this as function and use regex for find only words like below:

from collections import defaultdict
import re

def mxLenWord(words):
    dct = defaultdict(list)
    lstWrdSplt = re.findall('\w ', words)
    for word, length in (zip(lstWrdSplt,(map(len,lstWrdSplt)))):
        dct[length].append(word.strip())
    return dct[max(dct)]
    
words = 'rUnNiNg ,swimming, eating,biking, climbing'
mxLenWord(words)

Output:

['swimming', 'climbing']

CodePudding user response:

Try the below

from collections import defaultdict
data = defaultdict(list)
words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
for w in words.split(','):
  data[len(w)].append(w)
word_len = sorted(data.keys(),reverse=True)
for wlen in word_len:
  print(f'{wlen} -> {data[wlen]}')

output

9 -> ['abcdefghi', 'barracuda']
6 -> ['yellow']
5 -> ['shark']
4 -> ['barn', 'fish', 'swim']
3 -> ['run']

CodePudding user response:

There're plenty of methods which I find way too complicated for such an easy task. You can solve it using combination of sorted() and groupby():

from itertools import groupby

words = 'run,barn,abcdefghi,yellow,barracuda,shark,fish,swim'
_, [*longest] = next(groupby(sorted(words.split(","), key=len, reverse=True), len))
print(longest)

To find all words with same length you can use next one-liner:

from itertools import groupby

words = 'fishes,sam,gollum,sauron,frodo,balrog'
lengths = {l: list(w) for l, w in groupby(sorted(words.split(","), key=len), len)}
print(lengths)
  • Related