0
[solved] how to show both words if both words length are same ?
question :- print word from string which have maximum legnth a = "this is awesome feeling sufferi" list1 = a.split() d = {} for i in list1: b = len(i) d[b] = i print(d) max_i = max(d) print(d[max_i]) output :- sufferi in a string ...feeling and sufferi have same legnth ... but it prints only sufferi why?
5 Respostas
+ 3
Hemant Kosrekar
why it is printing only sufferi
because at last you are assigning the value of key 7 as sufferi
d={}
d[7] = "awesome"
d[7] = "feeling"
d[7] = "sufferi"
so last will be added to d
that's why it's printing sufferi
by the way you can print the max length word using max() function
print(max(input().split(), key=len))
Edit :
multi words of same length
https://code.sololearn.com/c30tCLX9nRBq/?ref=app
+ 2
a = "this is awesome feeling sufferi"
a = a.split()
B = []
Num = 0
For i in a:
If len(i) >= num :
Num = len(i)
B.append(i)
Print (*b,sep=' ')
0
a = "this is awesome feeling sufferi"
list1 = a.split()
long = 0
for i in range (len(list1)):
if len(list1[i]) > long:
long = list1[i]
print(long)
Hemant Kosrekar i edited it, it works now
0
Swift
it shows type error :
> operator not possible between str and int object
nice logic
but not works
0
Hemant Kosrekar an edited version
a = "this is awesome feeling sufferi"
list1 = a.split()
long = str(0)
for i in range (len(list1)):
if len(list1[i]) > len(long):
long = list1[i]
print(long)