Get Word Length In Dict Python
Solution 1:
A defaultdict
of set
s should do. First, let's define a function.
from collections import defaultdict
def get_word_counts(text):
d = defaultdict(set)
for word in text.split():
d[len(word)].add(word) # observe this bit carefully
return {k : list(v) for k, v in d.items()}
The idea is to find the length of each word, and insert it into the list/set that it belongs to. Once you've defined the function, you may call it as you please.
text = "May your coffee be strong and your Monday be short"print(get_word_counts(text))
{2: ['be'], 3: ['and', 'May'], 4: ['your'], 5: ['short'], 6: ['coffee', 'strong', 'Monday']}
Solution 2:
You can also use itertools.groupby
with sorted
in order to get similar "lazy" results:
a = 'a long list of words in nice'
x = groupby(sorted(a.split(), key=len), len) # word countsprint(dict((a, list(b)) for a, b in x))
>>> {1: ['a'], 2: ['of', 'in'], 4: ['long', 'list', 'nice'], 5: ['words']}
By "lazy" I mean that things would not start actually computing (e.g. if you have a really large string), until you start iterating over it. Be careful with iterators returned by groupby()
though! That is quite easy to accidentally empty them and then attempt to read second time (and get empty list).
The returned group is itself an iterator that shares the underlying iterable with groupby(). Because the source is shared, when the groupby() object is advanced, the previous group is no longer visible. So, if that data is needed later, it should be stored as a list
Post a Comment for "Get Word Length In Dict Python"