2 lines of python

Hi,

Please can you tell me what the last two lines in the python code below are doing?

data = open('input.txt', 'r').read() # should be simple plain text file
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
print 'data has %d characters, %d unique.' % (data_size, vocab_size)
char_to_ix = { ch:i for i,ch in enumerate(chars) }
ix_to_char = { i:ch for i,ch in enumerate(chars) }

This line:

char_to_ix = { ch:i for i,ch in enumerate(chars) }

creates a Dictionary called "char_to_ix" that has for its keys, each character from the list "chars". The corresponding values are numbers from 0 to len(chars)-1.

This line:

ix_to_char = { i:ch for i,ch in enumerate(chars) }

creates a Dictionary called "ix_to_char" that has the reverse key/value pairs as those of "char_to_ix". So each key of "ix_to_char" is a number from 0 to len(chars)-1 and its value is a character from the list "chars".

1 Like

Further to the above post, the last two lines use a python feature called list comprehension. I suggest you look that up in the Python documentation.

Andrew

1 Like