Python dictionary sequence Problem instance _python

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.