TLDR
Showing the results for a file named poem.txt
whose contents are:
a dog is by a cat to go hi
>>> with open('poem.txt', 'r') as file:
... words = file.read().split()
...
>>> [this_word for this_word in words if len(this_word) == len(max(words,key=len))]
['dog', 'cat']
Explanation
You can also make this faster by using the fact that <file-handle>.read.split()
returns a list
object and the fact that Python's max
function can take a function (as the keyword argument key
.) After that, you can use list comprehension to find multiple longest words.
Let's clarify that. I'll start by making a file with the example properties you mentioned,
For example, if the longest words were dog and cat your code should produce:
dog cat
{If on Windows - here I specifically use cmd
}
>echo a dog is by a cat to go hi > poem.txt
{If on a *NIX system - here I specifically use bash
}
$ echo "a dog is by a cat to go hi" > poem.txt
Let's look at the result of the <file-handle>.read.split()
call. Let's follow the advice of @MLP and use the with open
... as
statement.
{Windows}
>python
or possibly (with conda
, for example)
>py
{*NIX}
$ python3
From here, it's the same.
>>> with open('poem.txt', 'r') as file:
... words = file.read().split()
...
>>> type(words)
<class 'list'>
From the Python documentation for max
max(iterable, *[, key, default])
max(arg1, arg2, *args[, key])
Return the largest item in an iterable or the largest of two or more arguments.
If one positional argument is provided, it should be an iterable. The largest item in the iterable is returned. If two or more positional arguments are provided, the largest of the positional arguments is returned.
There are two optional keyword-only arguments. The key argument specifies a one-argument ordering function like that used for list.sort()
. The default argument specifies an object to return if the provided iterable is empty. If the iterable is empty and default is not provided, a ValueError
is raised.
If multiple items are maximal, the function returns the first one encountered. This is consistent with other sort-stability preserving tools such as sorted(iterable, key=keyfunc, reverse=True)[0]
and heapq.nlargest(1, iterable, key=keyfunc
).
New in version 3.4: The default keyword-only argument.
Changed in version 3.8: The key can be None.
Let's use a quick, not-so-robust way to see if we meet the iterable requirement (this SO Q&A gives a variety of other ways).
>>> hasattr(words, '__iter__')
True
Armed with this knowledge, and remembering the caveat, "If multiple items are maximal, the function returns the first one encountered.", we can go about solving the problem. We'll use the len
function (use >>> help(len)
if you want to know more).
>>> max(words, key=len)
'dog'
Not quite there. We just have the word. Now, it's time to use list comprehension to find all words with that length. First getting that length
>>> max_word_length = len(max(words, key=len))
>>> max_word_length
3
Now for the kicker.
>>> [this_word for this_word in words if len(this_word) == len(max(words,key=len))]
['dog', 'cat']
or, using the commands from before, and making things a bit more readable
>>> [this_word for this_word in words if len(this_word) == max_word_length]
['dog', 'cat']
You can use a variety of methods you'd like if you don't want the list format, i.e. if you actually want
dog cat
but I need to go, so I'll leave it where it is.