Python core programming version 2 Chapter 7 Exercise 1

Source: Internet
Author: User

7-3.
Dictionary and list methods.
(A) Create a dictionary and display the keys in the dictionary alphabetically.
(B) the keys and values in the dictionary are displayed in alphabetical order.
(C) Same as (B), but this time the keys and values in the dictionary are displayed based on the values in alphabetical order (NOTE: For the dictionary and hash table, this generally makes no practical sense, because most access and sorting (if needed) are dictionary-based keys. Here we only use it as an exercise ).
[Answer]
(A) The Code is as follows:
>>> My_dict = {'A': 4, 'B': 3, 'C': 2, 'D': 1}
>>> My_dict
{'A': 4, 'C': 2, 'B': 3, 'D': 1}
>>> My_dict.values ()
[4, 2, 3, 1]
>>> My_dict.keys ()
['A', 'C', 'B', 'D']
>>> Sorted (my_dict.keys ())
['A', 'B', 'C', 'D']
>>>
(B) The Code is as follows:
>>> For I in sorted (my_dict.keys ()):
... Print 'key = % s, value = % d' % (I, my_dict [I])
...
Key = a, value = 4
Key = B, value = 3
Key = c, value = 2
Key = d, value = 1
>>>
(C) The Code is as follows:
>>> Dic = {'A _ key': 5, 'B _ key': 4, 'c _ key': 3, 'd _ key': 2, 'E _ key': 1}
>>> K = sorted (dic. iteritems (), key = lambda asd: asd [1], reverse = False)
>>> For I in range (len (k): print k [I] [0], k [I] [1]
...
E_key 1
D_key 2
C_key 3
B _key 4
A_key 5
>>>

[Reference]
About function lambda

>>> Dic = {'A _ key': 5, 'B _ key': 4, 'c _ key': 3, 'd _ key': 2, 'E _ key': 1}
>>> Dic
{'E _ key': 1, 'B _ key': 4, 'A _ key': 5, 'd _ key': 2, 'c _ key ': 3}

>>> Print dic. iteritems ()
<Dictionary-itemiterator object at 0x00A75A50>
>>> Print type (dic. iteritems ())
<Type 'dictionary-itemiterator'>

>>> For I in dic. iteritems ():
... Print I
...
('E _ key', 1)
('B _ key', 4)
('A _ key', 5)
('D _ key', 2)
('C _ key', 3)

>>> For I in dic. iteritems ():
... Print I [0], I [1], I
...
E_key 1 ('e _ key', 1)
B _key 4 ('B _ key', 4)
A_key 5 ('A _ key', 5)
D_key 2 ('d _ key ', 2)
C_key 3 ('C _ key', 3)

>>> Print sorted (dic. iteritems ())
[('A _ key', 5), ('B _ key', 4), ('C _ key', 3), ('d _ key ', 2), ('e _ key', 1)]

>>> F = lambda x: x + 1
>>> F (2)
3
# What is a lambda function? X is the parameter, and x + 1 is the return value of the function.

>>> Print sorted (dic. iteritems (), key = lambda asd: asd [0])
[('A _ key', 5), ('B _ key', 4), ('C _ key', 3), ('d _ key ', 2), ('e _ key', 1)]

>>> Print sorted (dic. iteritems (), key = lambda asd: asd [1])
[('E _ key', 1), ('d _ key', 2), ('C _ key', 3), ('B _ key ', 4), ('A _ key', 5)]

>>> Print sorted (dic. iteritems (), key = lambda asd: asd [1], reverse = True)
[('A _ key', 5), ('B _ key', 4), ('C _ key', 3), ('d _ key ', 2), ('e _ key', 1)]

>>> Print sorted (dic. iteritems (), key = lambda asd: asd [1], reverse = False)
[('E _ key', 1), ('d _ key', 2), ('C _ key', 3), ('B _ key ', 4), ('A _ key', 5)]

Python dictionary sorting
Http://hi.baidu.com/when_love/blog/item/b66b510143821709728da563.html

Python list sorting sort
Http://wupinyin.appspot.com/article_detail? Id = agh3dXBpbnlpbnIOCxIHQXJ0aWNsZRjyLgw

7-4.
Create a dictionary. Two lists with the same length are given. For example, the list is [1, 2, 3,...]. and ['abc', 'def', 'ghi',...], use all the data in these two lists to form a dictionary. Like this: {1: 'abc', 2: 'def ', 3: 'ghi ',...}.
[Answer]
The Code is as follows:
>>> List_a = [1, 2, 3, 4]
>>> List_ B = ['A _ value', 'B _ value', 'c _ value', 'd _ value']

>>> Zip (list_a, list_ B)
[(1, 'A _ value'), (2, 'B _ value'), (3, 'c _ value'), (4, 'd _ value')]

>>> C = {}

>>> K = zip (list_a, list_ B)
>>> K
[(1, 'A _ value'), (2, 'B _ value'), (3, 'c _ value'), (4, 'd _ value')]

>>> For I in range (len (k )):
... C. update ({k [I] [0]: k [I] [1]})
...

>>> C
{1: 'A _ value', 2: 'B _ value', 3: 'C _ value', 4: 'd _ value '}

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.