optimization - Is it more efficient to create a dictionary in python using zip or a list comprehension? -
lets want set basic text encoding using dictionary in python.
two ways of doing come mind - using zip, , using list comprehension.
characters = "abcdefghijklmnopqrstuvwxyz .,!;" dict_a = dict((x, characters[x]) x in xrange(0, 31)) dict_b = dict(zip(xrange(0, 31), characters))
which of these more efficient? (the real encoding longer 31, toy example). difference significant?
alternatively, approaching wrong , should using other dictionary? (i need able go in both directions of encoding).
the enumerate
function easiest way create dict
:
dict_c = dict(enumerate(characters))
however, i'm not sure gives you can't string. following seem equivalent me:
>>> dict_c[3] 'd' >>> characters[3] 'd'
Comments
Post a Comment