python字典

字典的定义

字典是python中唯一的映射类型(哈希表)。
字典对象是可变的,但字典的键必须使用不可变对象,一个字典可以使用不同类型的键值。字典的输出是无序的。

1
2
3
4
5
6
7
8
9
In [1]: dic = {}

In [2]: type(dic)
Out[2]: dict

In [3]: dic = {'a':1,1:123}

In [4]: dic
Out[4]: {1: 123, 'a': 1}

字典的方法

  • keys()
    返回列表,内容为字典的key。

    1
    2
    3
    4
    In [5]: dic = {'a':1,1:123,('a','b'):'hello'}

    In [6]: dic.keys()
    Out[6]: ['a', 1, ('a', 'b')]
  • values()
    返回列表,内容为字典中的value。

    1
    2
    3
    4
    In [5]: dic = {'a':1,1:123,('a','b'):'hello'}

    In [7]: dic.values()
    Out[7]: [1, 123, 'hello']
  • items()
    返回列表,内容为字典的键值对。

    1
    2
    3
    4
    In [5]: dic = {'a':1,1:123,('a','b'):'hello'}

    In [9]: dic.items()
    Out[9]: [('a', 1), (1, 123), (('a', 'b'), 'hello')]

字典练习

从键盘读取输入,存入字典,输出。

输出方式1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@linux02 ~]# cat zidian.py
#!usr/bin/python

info = {}
name = raw_input('Please input your name:')
age = raw_input('Please input your age:')
gender = raw_input('Please input your gender(M/F):')
info['name'] = name
info['age'] = age
info['gender'] = gender
print info
[root@linux02 ~]# python zidian.py
Please input your name:mike
Please input your age:20
Please input your gender(M/F):M
{'gender': 'M', 'age': '20', 'name': 'mike'}

输出方式2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@linux02 ~]# cat zidian.py
#!usr/bin/python

info = {}
name = raw_input('Please input your name:')
age = raw_input('Please input your age:')
gender = raw_input('Please input your gender(M/F):')
info['name'] = name
info['age'] = age
info['gender'] = gender
for i in info.items():
print i
print 'main end'
[root@linux02 ~]# python zidian.py
Please input your name:tom
Please input your age:20
Please input your gender(M/F):M
('gender', 'M')
('age', '20')
('name', 'tom')
main end

输出方式3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@linux02 ~]# cat zidian.py
#!usr/bin/python

info = {}
name = raw_input('Please input your name:')
age = raw_input('Please input your age:')
gender = raw_input('Please input your gender(M/F):')
info['name'] = name
info['age'] = age
info['gender'] = gender
for k,v in info.items():
print k,v
print 'main end'
[root@linux02 ~]# python zidian.py
Please input your name:Mike
Please input your age:33
Please input your gender(M/F):M
gender M
age 33
name Mike
main end

输出方式4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@linux02 ~]# cat zidian.py
#!usr/bin/python

info = {}
name = raw_input('Please input your name:')
age = raw_input('Please input your age:')
gender = raw_input('Please input your gender(M/F):')
info['name'] = name
info['age'] = age
info['gender'] = gender
for k,v in info.items():
print '%s: %s' %(k,v)
print 'main end'
[root@linux02 ~]# python zidian.py
Please input your name:Mike
Please input your age:33
Please input your gender(M/F):M
gender: M
age: 33
name: Mike
main end

字典实例

  • 现有一个字典dict1 保存的是小写字母a-z对应的ASCII码
    dict1 = {‘a’: 97, ‘c’: 99, ‘b’: 98, ‘e’: 101, ‘d’: 100, ‘g’: 103, ‘f’: 102, ‘i’: 105, ‘h’: 104, ‘k’: 107, ‘j’: 106, ‘m’: 109, ‘l’: 108, ‘o’: 96, ‘n’: 110, ‘q’: 113, ‘p’: 112, ‘s’: 115, ‘r’: 114, ‘u’: 117, ‘t’: 116, ‘w’: 119, ‘v’: 118, ‘y’: 121, ‘x’: 120, ‘z’: 122}
    1) 将该字典按照ASCII码的值排序
    2) 有一个字母的ASCII错了,修改为正确的值,并重新排序
    1
    2
    3
    In [1]: dict1 = {'a': 97, 'c': 99, 'b': 98, 'e': 101, 'd': 100, 'g': 103, 'f': 102, 'i': 105, 'h': 104, 'k': 107, 'j': 106, 'm': 109, 'l': 108, 'o': 96, 'n': 110, 'q': 113, 'p': 112, 's': 115, 'r': 114, 'u': 117, 't': 116, 'w': 119, 'v': 118, 'y': 121, 'x': 120, 'z': 122}
    In [3]: print sorted(dict1.items(), key=lambda d:d[1])
    [('o', 96), ('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101), ('f', 102), ('g', 103), ('h', 104), ('i', 105), ('j', 106), ('k', 107), ('l', 108), ('m', 109), ('n', 110), ('p', 112), ('q', 113), ('r', 114), ('s', 115), ('t', 116), ('u', 117), ('v', 118), ('w', 119), ('x', 120), ('y', 121), ('z', 122)]
1
2
3
4
5
for i,j in dict1.items():
if j != ord(i):
print("{0}'s value is not correct,shoud be {1}".format(i, ord(i)))
dict1[i] = ord(i)
print sorted(dict1.items(), key=lambda d:d[1])

输出结果

1
2
o's value is not correct,shoud be 111
[('a', 97), ('b', 98), ('c', 99), ('d', 100), ('e', 101), ('f', 102), ('g', 103), ('h', 104), ('i', 105), ('j', 106), ('k', 107), ('l', 108), ('m', 109), ('n', 110), ('o', 111), ('p', 112), ('q', 113), ('r', 114), ('s', 115), ('t', 116), ('u', 117), ('v', 118), ('w', 119), ('x', 120), ('y', 121), ('z', 122)]

  • 用最简洁的代码,自己生成一个大写字母 A-Z 及其对应的ASCII码值的字典dict2(使用dict,zip,range方法)

    1
    2
    3
    4
    In [10]: dict2=dict(zip(string.uppercase, range(ord('A'), ord('Z')+1)))

    In [11]: print dict2
    {'A': 65, 'C': 67, 'B': 66, 'E': 69, 'D': 68, 'G': 71, 'F': 70, 'I': 73, 'H': 72, 'K': 75, 'J': 74, 'M': 77, 'L': 76, 'O': 79, 'N': 78, 'Q': 81, 'P': 80, 'S': 83, 'R': 82, 'U': 85, 'T': 84, 'W': 87, 'V': 86, 'Y': 89, 'X': 88, 'Z': 90}
  • 将dict2与第一题排序后的dict1合并成一个dict3

    1
    2
    3
    4
    5
    6
    In [8]: dict2.update(dict1)

    In [9]: dict3=dict2.copy()

    In [10]: print dict3
    {'A': 65, 'C': 67, 'B': 66, 'E': 69, 'D': 68, 'G': 71, 'F': 70, 'I': 73, 'H': 72, 'K': 75, 'J': 74, 'M': 77, 'L': 76, 'O': 79, 'N': 78, 'Q': 81, 'P': 80, 'S': 83, 'R': 82, 'U': 85, 'T': 84, 'W': 87, 'V': 86, 'Y': 89, 'X': 88, 'Z': 90, 'a': 97, 'c': 99, 'b': 98, 'e': 101, 'd': 100, 'g': 103, 'f': 102, 'i': 105, 'h': 104, 'k': 107, 'j': 106, 'm': 109, 'l': 108, 'o': 111, 'n': 110, 'q': 113, 'p': 112, 's': 115, 'r': 114, 'u': 117, 't': 116, 'w': 119, 'v': 118, 'y': 121, 'x': 120, 'z': 122}

Recommended Posts