Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with string practice
#1
Hi everyone,
I'm looking for some help with an exercise I was doing, regarding strings in python.
This program is supposed to find the longest word out of a list of words the user types (i.e. if I type "chicken, dog and cat" the output will be "chicken"), but for the sake of a possibility, if I did type, for example, only "dog and cat", the output will always be dog. Thing is, I want my program to print them both.
Here is the code:

def longest_word(l):
    lenl=[]
    for n in l:
        lenl.append((len(n), n))
    lenl.sort()
    return(lenl[-1][1])
lis=input("Insert some words ").replace(",", " ").replace("and", " ")
lispar=lis.split(" ")
print(longest_word(lispar))
Thanks in advance! ( Oh, and if you find anything I could improve here, furthermore, just let me know, it's very appreciated Tongue )
Reply
#2
You have the longest word and you know it's length (len[-1][0]). You need to gather all the words with that length:

def longest_word(l):
    lenl=[]
    for n in l:
        lenl.append((len(n), n))
    lenl.sort(reverse = True)  # Reversing makes the next part easier.
    longest = []
    for length, word in lenl:
        if length == lenl[0][0]:
            longest.append(word)
        else:
            break
    return(list(reversed(longest)))  # Revesing again to put them back in order.
lis=input("Insert some words ").replace(",", " ").replace("and", " ")
lispar=lis.split(" ")
print(longest_word(lispar))
A handy thing when building lists like this is a list comprehension:

output = []
for item in data:
    if condition:
        output.append(function(item))

# is equivalent to

output = [function(item) for item in data if condition]
So we can redo the above code as:

def longest_word(l):
    lenl=[(len(word), word) for word in l]
    lenl.sort(reverse = True)
    longest = [word for length, word in lenl if length == lenl[0][0]]
    return(longest[::-1])  # Another way to revese a list.
lis=input("Insert some words ").replace(",", " ").replace("and", " ")
lispar=lis.split(" ")
print(longest_word(lispar))
Other advice: Avoid single letter variable names. Descriptive, full word names make your code more readable. 'longest_word' is a good name, stick to things like that.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Thank you very much for the explanation! Have a good day :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Best practice on using virtual environments in Python bytecrunch 6 643 Feb-14-2024, 03:22 PM
Last Post: snippsat
  best practice for import libraries and using pyinstaller aster 3 2,802 Apr-17-2021, 11:12 AM
Last Post: snippsat
  Threading best practice EvanS1 2 1,888 Apr-21-2020, 10:11 PM
Last Post: EvanS1
  How to properly close db connection, best practice t4keheart 6 2,930 Jan-24-2020, 06:58 PM
Last Post: Marbelous
  Best Practice For String Quotations ? Zork_3 9 49,837 Sep-01-2017, 07:16 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020