Python lexicographically problematic instances and python dictionary instances

Source: Internet
Author: User
Tags pear

Python lexicographically problematic instances and python dictionary instances

This article describes the python lexicographic issue and shares it with you for your reference. The details are as follows:

Problem description:

The order of letters from left to right is the same as that in the alphabet, and each character can appear up to once .. for example, a, B, AB, bc, and xyz are all ascending strings. sort all ascending strings of the letter A with A length not greater than 6 in alphabetical order and encode them as follows:

1 2 .. 26 27 28 ...
A B .. Z AB Ac ..

Quickly calculate the encoding of an ascending string in the preceding 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 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 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            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:]]  list_num_five = [ i + j + k + l + m 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:]]  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()value_to_get = 'abcdef'for key,value in my_dict.iteritems():  if value == value_to_get:    print key

Result: 83682

That is, the abcdef encoding in the dictionary.

I hope this article will help you with Python programming.


How to sort Python dictionaries, for example

In Python2.7.x, OrderedDict is added to the collections class. The usage is as follows:
 

In Python2.7.x, OrderedDict is added to the collections class. The usage is as follows:

Pywugw @ pywugw-laptop :~ $/Usr/local/bin/python2.7
Python 2.7b1 (r27b1: 79927, Apr 26 2010, 11:44:19)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> From collections import OrderedDict
>>> D = {'banana ': 3, 'apple': 4, 'pear': 1, 'Orange ': 2}

# Sort by key
>>> OrderedDict (sorted (d. items (), key = lambda t: t [0])
OrderedDict ([('apple', 4), ('banana ', 3), ('Orange', 2), ('pear ', 1)])

# Sort by value
>>> OrderedDict (sorted (d. items (), key = lambda t: t [1])
OrderedDict ([('pear ', 1), ('Orange', 2), ('banana ', 3), ('apple', 4)])
# Sort by key length
>>> OrderedDict (sorted (d. items (), key = lambda t: len (t [0])
OrderedDict ([('pear ', 1), ('apple', 4), ('Orange', 2), ('banana ', 3)])

Sort dictionaries in python

>>> D
{'A': 1, 'World': 11, 'z': 9, 'Hello': 10}
>>> K = d. keys ()
>>> K. sort ()
>>> K
['A', 'Hello', 'World', 'z']
>>> T = map (lambda key :( key, d [key]), k)
>>> T
[('A', 1), ('hello', 10), ('World', 11), ('Z', 9)]

Related Article

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.