Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Split list based on first character of word

Im kind of stuck on an issue and Ive gone round and round with it until ive confused myself.

What I am trying to do is take a list of words:

['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']

Then sort them under and alphabetical order:

A
About
Absolutely
After

B
Bedlam
Behind

etc...

Is there and easy way to do this?

like image 735
JasonOrtiz Avatar asked Dec 16 '22 09:12

JasonOrtiz


2 Answers

Use itertools.groupby() to group your input by a specific key, such as the first letter:

from itertools import groupby
from operator import itemgetter

for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
    print letter
    for word in words:
        print word
    print

If your list is already sorted, you can omit the sorted() call. The itemgetter(0) callable will return the first letter of each word (the character at index 0), and groupby() will then yield that key plus an iterable that consists only of those items for which the key remains the same. In this case that means looping over words gives you all items that start with the same character.

Demo:

>>> somelist = ['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone']
>>> from itertools import groupby
>>> from operator import itemgetter
>>> 
>>> for letter, words in groupby(sorted(somelist), key=itemgetter(0)):
...     print letter
...     for word in words:
...         print word
...     print
... 
A
About
Absolutely
After
Aint
Alabama
AlabamaBill
All
Also
Amos
And
Anyhow
Are
As
At
Aunt
Aw

B
Bedlam
Behind
Besides
Biblical
Bill
Billgone
like image 140
Martijn Pieters Avatar answered Dec 17 '22 22:12

Martijn Pieters


Instead of using any library imports, or anything fancy. Here is the logic:

def splitLst(x):
    dictionary = dict()
    for word in x:
       f = word[0]
       if f in dictionary.keys():
            dictionary[f].append(word)
       else:
            dictionary[f] = [word]
     return dictionary

splitLst(['About', 'Absolutely', 'After', 'Aint', 'Alabama', 'AlabamaBill', 'All', 'Also', 'Amos', 'And', 'Anyhow', 'Are', 'As', 'At', 'Aunt', 'Aw', 'Bedlam', 'Behind', 'Besides', 'Biblical', 'Bill', 'Billgone'])
like image 45
Mike Avatar answered Dec 17 '22 23:12

Mike