Hugo Arts <hugo.yo...@gmail.com> dixit: > bc = {y: x for x, y in enumerate("khalid")} > > Note that your output is like so: > {'a': 2, 'd': 5, 'i': 4, 'h': 1, 'k': 0, 'l': 3} > > The first character in your original string gets a zero, the second a > one, so on and so forth. I'm hoping that's what you meant. If you > really want this: > > {'a': 0, 'd': 1, 'i': 2, 'h': 3, 'k': 4, 'l': 5} > > I'm not sure how to do that programmatically.
# first need a list of sorted chars # otherwise python cannot guess what order you mean: chars = sorted(list("khalid")) print chars # ==> ['a', 'd', 'h', 'i', 'k', 'l'] # enumerate gives a list of (index, value) pairs # from which you can construct a dict: #~ dc = {index:char for (index,char) in enumerate(chars)} # or (python version < 3) dc = dict((index,char) for (index,char) in enumerate(chars)) print dc # ==> {0: 'a', 1: 'd', 2: 'h', 3: 'i', 4: 'k', 5: 'l'} Denis ________________________________ la vita e estrany http://spir.wikidot.com/ _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor