0

I am trying to find the largest and smallest word in a given string.

This is my target string.

'Hello, welcome to python programming '

I have extracted the words in the string and their lengths into key value pairs in a dictionary. Below is the code i wrote.

Line='Hello, welcome to python programming '

words=[x for x in Line.split()]
print(words)

lengths=[len(word) for word in words]
print(lengths)

x = {words[i]: lengths[i] for i in range(len(words))} 
print('--'*44)
print (x)    

I am getting the resulted dictionary as below

['Hello,', 'welcome', 'to', 'python', 'programming']
[6, 7, 2, 6, 11]
*************************************************************************
{'Hello,': 6, 'welcome': 7, 'to': 2, 'python': 6, 'programming': 11}

I want to sort the dictionary and print the keys which have the highest and lowest values i.e the words which are longest and shortest.

Patronus X24
  • 1
  • 1
  • 2
  • 2
    Do you want to carry on with your current approach or would also want an alternative? – DirtyBit Apr 17 '19 at 10:07
  • `[x for x in Line.split()]` is extremely redundant. You could expand that to `[y for y in [x for x in Line.split()]]`, or reduce it to just `Line.split()`, for exactly the same result… – deceze Apr 17 '19 at 10:09
  • check this https://stackoverflow.com/a/613218/5094841 – VnC Apr 17 '19 at 10:13

4 Answers4

2

you could also abuse collections.Counter:

from collections import Counter

line = 'Hello, welcome to python programming '
c = Counter({word: len(word) for word in line.split()})

print(c.most_common(1)[0])  # ('programming', 11)
print(c.most_common()[-1])  # ('to', 2)
hiro protagonist
  • 44,693
  • 14
  • 86
  • 111
1

If you want an oneliner list comprehension, try this :

l = 'Hello, welcome to python programming '
print(*[i for i in l.split() if len(i) == max([len(k) for k in l.split()])]) # Gives longest word (words if multiple)
print(*[i for i in l.split() if len(i) == min([len(k) for k in l.split()])]) # Gives shortest word (words if multiple)

OUTPUT :

programming
to

If you want to go with your approach, try this :

x = {'Hello,': 6, 'welcome': 7, 'to': 2, 'python': 6, 'programming': 11}
sorted_x = sorted(x, key= lambda y: len(y), reverse = True)
longest_word = sorted_x[0]
shortest_word = sorted_x[-1]
Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
1

Using your current approach:

Using max() min()

maxWord = max(x, key=x.get)
minWord = min(x, key=x.get)

print("The maximum-sized word is {} with len {}".format(maxWord, x[maxWord]))
print("The minimum-sized word is {} with len {}".format(minWord, x[minWord]))

OUTPUT:

The maximum-sized word is programming with len 11
The minimum-sized word is to with len 2

Alternative:

s = 'Hello, welcome to python programming '

print(max(s.split(), key=len))
print(min(s.split(), key=len))

Elaborated:

s = 'Hello, welcome to python programming '
maxWord = max(s.split(), key=len)
maxLen = len(maxWord)

minWord = min(s.split(), key=len)
minLen = len(minWord)

print((maxWord, maxLen))           # ('programming', 11)
print((minWord, minLen))           # ('to', 2)

One-liner:

print((((max(s.split(), key=len), len(maxWord)),(min(s.split(), key=len), len(maxWord)))))

OUTPUT:

(('programming', 11), ('to', 11))
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
0
a = "  i love flask"
a = a.split()
b = len(a[0])
c = 0
for x in a:
    s = len(x)
    if s < b:
      b = s
    elif s > c:
      c = s 
for y in a:
  k = len(y)
  if k == b:
    print (b,y)
  elif  k == c:
    print (c,y)
vishesh
  • 1
  • 1