Hi
I need to Write a function which takes as input two strings and count how many words are both in the two
strings. Display the results sorted according the length of the word (longer first) and in case of
equality by the alphabetical order?

## Recommended Answers

What have you got so far?

Try ...
wordlist.sort()
then ...
wordlist.sort(key=len)

## All 8 Replies

What have you got so far?

Hint ...
sentence.split() will give you a list of words.

Try ...
wordlist.sort()
then ...
wordlist.sort(key=len)

Thanks vegaseat
Im working with C# now Im back to python.

I tried but I got wrong result,Whats wrong in my code?
Show me another way also please.

``````import string
def count_word():
s1 = "Hello World"
s2 = "Computer Programming for Kids and Other Beginners"
numCount = 0
for word in s1 and s2:
words = word.split()
numCount += 1
print "Total strings is", numCount

count_word()

#def sort_word():

""">>> ================================ RESTART ================================
>>>
Total strings is 49
>>> """
``````

hmm you have ask similar question before and got good answers.
So two word in s1.

``````>>> s1 = "Hello World"
>>> len(s1.split())
2
``````

Some really basic mistake.
What happends if you iterate before you split() like you do?

``````s1 = "Hello World"
for word in s1:
print word #?
``````

`s1 and s2` this dos dos not mean concatenate s1 with s2.
`and or not` are logical operators,is in every Python tutorial/book.
`and`,If both the operands are true then then condition becomes true.

Fix.

``````def count_word():
s1 = "Hello World"
s2 = "Computer Programming for Kids and Other Beginners"
numCount = 0
for word in s1.split() + s2.split():
numCount += 1
print "Total strings is", numCount

count_word()
``````

Better.

``````s1 = "Hello World"
s2 = "Computer Programming for Kids and Other Beginners"
total_words = s1.split() + s2.split()

print len(total_words)
sort_key = lambda s: (-len(s), s) #in case of equality by the alphabetical order?
print sorted(total_words, key=sort_key)
``````

snippsat,thanks for beautiful explain
I do also like this but its the same.

``````import string
def count_word():
s1 = "Hello World"
s2 = "Computer Programming for Kids and Other Beginners"
total_count = 0
for word in s1.split() + s2.split():
total_count = len(word)

print "Total strings is :", total_count

count_word()
``````
in case of equality by the alphabetical order?

Can we use another way instead lambda?

How mean vegaseat wordlist.sort(key=len) ?

How mean vegaseat wordlist.sort(key=len) ?

Try it out with interactive interpreter.
`sort()` dos a inplace sort of orginal list,`sorted()` generate a new list so you can store it in variable.

``````>>> lst = ['a', 'aaaa' ,'bb', 'aa', 'aaaaaaaaaa']
>>> lst.sort(key=len)
>>> lst
['a', 'bb', 'aa', 'aaaa', 'aaaaaaaaaa']
>>> lst.sort(key=len, reverse=True)
>>> lst
['aaaaaaaaaa', 'aaaa', 'bb', 'aa', 'a']
``````

So here it sort by lenght,but a problem `bb` should be behind `aa`.
So here as vegaseat advised.

``````>>> lst = ['a', 'aaaa' ,'bb', 'aa', 'aaaaaaaaaa']
>>> lst.sort()
>>> lst
['a', 'aa', 'aaaa', 'aaaaaaaaaa', 'bb']
>>> lst.sort(key=len, reverse=True)
>>> lst
['aaaaaaaaaa', 'aaaa', 'aa', 'bb', 'a']
``````

Thanks again snippsat for your explain.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.