This article describes the Python dictionary sequence problem, share to everyone for your reference. Specific as follows:
Problem Description:
The order of the letters from left to right is the same as the order in the alphabet, and each character appears most once. For example: A,B,AB,BC,XYZ and so on are all ascending strings. An ascending string of all lengths not exceeding 6 for the alphabet A is now sorted by dictionary and encoded as follows:
| 1 |
2 |
.. |
26 |
27 |
28 |
... |
| A |
B |
.. |
Z |
Ab |
Ac |
.. |
For an ascending string, quickly calculate its encoding in the above dictionary.
The implementation code is as follows:
Import Stringall_letter = String.ascii_lowercasedef gen_dict (): result = {} List_num_one = [A_letter for A_letter in AL L_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 in All_letter for J in All_letter[all_letter.find (i) + 1:] for K in All_letter[all_letter. Find (j) + 1:]] List_num_four = [i + j + k + L for I in All_letter to J in All_letter[all_letter.find (i) + 1:] For K in All_letter[all_letter.find (j) + 1:] for L in All_letter[all_letter.find (k) + 1:]] list_num_five = [i + j + k + L + M for I in All_letter to J in All_letter[all_letter.find (i) + 1:] for K in All_lette R[all_letter.find (j) + 1:] for L in All_letter[all_letter.find (k) + 1:] for M in All_letter[all_letter.fin D (L) + 1:]] List_num_six = [i + j + k + L + M + N for i in All_letter for J in All_letter[all_letter.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_letter.find (l) + 1:] For N in All_letter[all_letter.find (m) + 1:] "for key,value in enumerate (list_num_one + list_num_two + list_num_three + list_num_four + list_num_five + list_num_six): Result.setdefault (key+1,value) return result my_dict = gen_dict () va Lue_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.
Hopefully this article will help you with Python programming.