The example of this article tells the Python dictionary sequence question, shares for everybody to consult. Specifically as follows:
Problem Description:
The order of the letters from left to right is the same as in the alphabet, and each character appears one at a time. For example: A,B,AB,BC,XYZ and so on are all ascending strings. An ascending string of all lengths not exceeding 6, which is produced by the alphabet A, is filled in the dictionary and encoded as follows:
| 1 |
2 |
.. |
26 |
27 |
28 |
... |
| A |
B |
.. |
Z |
Ab |
Ac |
.. |
For an ascending string, quickly compute its encoding in the dictionary above.
The implementation code is as follows:
Import String all_letter = String.ascii_lowercase def gen_dict (): result = {} List_num_one = [A_letter for A_letter In all_letter] List_num_two = [i + j for I in All_letter for J in All_letter[all_letter.find (i) + 1:]] List_num_three = [i + j + K for I-All_letter for J-All_letter[all_letter.find (i) + 1:] for K in All_letter[al L_letter.find (j) + 1:]] List_num_four = [i + j + k + L for I into All_letter for J in All_letter[all_letter.fin D (i) + 1:] for K in All_letter[all_letter.find (j) + 1:] for L in All_letter[all_letter.find (k) + 1:]] Li St_num_five = [i + j + k + L + M for I-All_letter for J in All_letter[all_letter.find (i) + 1:] fo R K in All_letter[all_letter.find (j) + 1:] for L in All_letter[all_letter.find (k) + 1:] for M in All_let Ter[all_letter.find (L) + 1:]] List_num_six = [i + j + k + L + M + N for I into All_letter for J in All_letter[all_le Tter.find (i) + 1:] For K in All_letter[all_letter.find (j) + 1:] for L in All_letter[all_letter.find (k) + 1:] for M in all_letter[all_l Etter.find (L) + 1:] for N in All_letter[all_letter.find (m) + 1:]] for Key,value in enumerate (List_num_one + Lis T_num_two + list_num_three + list_num_four + list_num_five + list_num_six): Result.setdefault (Key+1,value) return re
Sult my_dict = gen_dict () value_to_get = ' abcdef ' for Key,value in My_dict.iteritems (): if value = = Value_to_get:
Print key
Results: 83682
That is, the encoding of abcdef in the dictionary.
I hope this article will help you with your Python programming.