Creating a dictionary from a text file

Ok, I'm trying to create a dictionary from a text file, so the key is one lowercase character and each value is a list of words from the file starting with that letter.

The text file contains one line word per line, for example:

airport
bathroom
boss
bottle
elephant

      

Output:

words = {'a': ['airport'], 'b': ['bathroom', 'boss', 'bottle'], 'e':['elephant']}

      

Havent gotten very much, just got confused about how I would get the first index from each row and put it as a key and add the values. it would be really helpful if someone can help me figure it out.

words = {}

for line in infile:
  line = line.strip() # not sure if this line is correct

      

+3


source to share


2 answers


So let's take a look at your example:

words = {}
for line in infile:
  line = line.strip()

      

This looks good to start with. Now you want to do something with line

. You will probably need the first character, which you can access via line[0]

:

  first = line[0]

      

Then you want to check if the letter is already in the dict. If not, you can add a new empty list:

  if first not in words:
    words[first] = []

      

Then you can add a word to this list:



  words[first].append(line)

      

And you're done!

If the strings are already sorted like in your example, you can also use itertools.groupby

, which is a little more complicated

from itertools import groupby
from operator import itemgetter

with open('infile.txt', 'r') as f:
  words = { k:map(str.strip, g) for k, g in groupby(f, key=itemgetter(0)) }

      

You can also sort the strings first, which makes this method generally accepted:

groupby(sorted(f), ...)

      

+2


source


defaultdict

from the module collections

is a good choice for tasks like this:



>>> import collections
>>> words = collections.defaultdict(list)
>>> with open('/tmp/spam.txt') as f:
...   lines = [l.strip() for l in f if l.strip()]
... 
>>> lines
['airport', 'bathroom', 'boss', 'bottle', 'elephant']
>>> for word in lines:
...   words[word[0]].append(word)
... 
>>> print words
defaultdict(<type 'list'>, {'a': ['airport'], 'b': ['bathroom', 'boss', 'bottle'], 'e': ['elephant']})

      

+1


source







All Articles