-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathdict_basic.py
More file actions
239 lines (186 loc) · 5.66 KB
/
dict_basic.py
File metadata and controls
239 lines (186 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# _*_ coding:utf-8 _*_
"""
This file is some operator about dict
cited:
https://stackoverflow.com/questions/613183/how-to-sort-a-dictionary-by-value
https://stackoverflow.com/questions/72899/how-do-i-sort-a-list-of-dictionaries-by-values-of-the-dictionary-in-python
http://pythoncentral.io/how-to-sort-python-dictionaries-by-key-or-value/
https://stackoverflow.com/questions/20464368/sort-a-dictionary-by-length-of-the-value
"""
import operator
from collections import OrderedDict
def lst_2_dict():
"""
combine two list to dict
:return:
"""
lst1 = ['a', 'b', 'c']
lst2 = [1, 2, 3]
d1 = {k: v for k, v in zip(lst1, lst2)}
print d1
# {'a': 1, 'c': 3, 'b': 2}
d2 = dict(zip(lst1, lst2))
print d2
# {'a': 1, 'c': 3, 'b': 2}
# d.pop('a')
# del d['a']
# print d
def iter_dict():
"""
RuntimeError: dictionary changed size during iteration
iteration del error
"""
d_info = {'aa': -1, 'bb': 0, 'cc': 1, 'dd': 2}
for key in d_info.iterkeys():
print key, d_info[key]
if d_info[key] < 0:
# RuntimeError: dictionary changed size during iteration
d_info.pop(key)
# del d_info[key]
pass
print 'after del'
for key in d_info.iterkeys():
print key, d_info[key]
def iter_dict_remove():
"""remove item in dict while iteration it"""
d_info = {'aa': -1, 'bb': 0, 'cc': 1, 'dd': 2}
for k in d_info.keys():
print k, d_info[k]
if d_info[k] < 0:
del d_info[k]
print 'after del', d_info
def iter_dic_sort():
"""iter dict by sorted keys"""
d_info = {'33': 33, '88': 88, '22': 22, '44': 44}
for k in sorted(d_info):
print k, d_info[k]
def dict_sort_by_value():
dic_num = {'first': 11, 'second': 2, 'third': 33, 'Fourth': 4}
# print all the keys
print dic_num.keys()
print list(dic_num)
# ['second', 'Fourth', 'third', 'first']
# print all the sorted keys
print sorted(dic_num)
# ['Fourth', 'first', 'second', 'third']
print sorted(dic_num, key=str.lower)
# ['first', 'Fourth', 'second', 'third']
# print sorted values
print sorted(dic_num.values())
# [2, 4, 11, 33]
# sorted by value
sorted_val = sorted(dic_num.items(), key=operator.itemgetter(1))
# [('second', 2), ('Fourth', 4), ('first', 11), ('third', 33)]
print sorted_val
# sorted by key
sorted_key = sorted(dic_num.items(), key=operator.itemgetter(0))
print sorted_key
# [('Fourth', 4), ('first', 11), ('second', 2), ('third', 33)]
dic_k_lst = {'11': [1, 2], 'ab': [3], 'cd': [0, -1, 2]}
# Sort a dictionary by length of the value
print sorted(dic_k_lst.items(), key=lambda item: len(item[1]))
# [('ab', [3]), ('11', [1, 2]), ('cd', [0, -1, 2])]
dic_k_set = {'11': {1, 2}, 'ab': {3}, 'cd': {0, -1, 2}}
# Sort a dictionary by length of the value
d_val_len = sorted(dic_k_set.items(), key=lambda item: len(item[1]))
print d_val_len
# [('ab', set([3])), ('11', set([1, 2])), ('cd', set([0, 2, -1]))]
print d_val_len[0][1]
# set([3])
print len(d_val_len[0][1])
# 1 (长度为 1)
def dic_to_lst():
dic_k_lst = {'11': [1, 2], 'ab': [3], 'cd': [0, -1, 2]}
lst = [(k, len(dic_k_lst[k])) for k in dic_k_lst.keys()]
print lst
# [('11', 2), ('ab', 1), ('cd', 3)]
def dic_count_value():
from collections import Counter
dic_k_lst = {'11': None, 'ab': 3, 'name': 'xy', 'none': None, 'age': 26}
print Counter(dic_k_lst.values())
# Counter({None: 2, 'xy': 1, 26: 1, 3: 1})
count_none = 0
count_not_none = 0
for k in dic_k_lst.keys():
if dic_k_lst[k] is None:
count_none += 1
else:
count_not_none += 1
print count_none, count_not_none
# 2 3
def count_val_lst_len():
d = {'T1': ['eggs', 'bacon', 'sausage'], 'T2': ['spam', 'ham', 'monty', 'python']}
print map(len, d.values())
# [4, 3]
print sum(map(len, d.values()))
# 7
def sum_dic_val():
d_info = {'33': 1, '88': 2, '22': 3, '44': 4}
print sum(d_info.values())
# 10
def dict_pop():
dic_k_lst = {'11': {1, 2}, 'ab': [3], 'cd': [0, -1, 2]}
s1 = dic_k_lst['11']
print s1
# set([1, 2])
# del dic_k_lst['11']
dic_k_lst.pop('11')
print s1
# set([1, 2])
print dic_k_lst
# {'ab': [3], 'cd': [0, -1, 2]}
def dic_val_lst():
dic_k_lst = {'11': [1, 2], 'ab': [3], 'cd': [0, -1, 2]}
dic_k = {}
for k, v in dic_k_lst.items():
for item in v:
dic_k[item] = k
print dic_k
def dic_probability():
p = {1: {1: 0.1, 2: 0.3}, 2: {1: 0.1, 2: 0.3}}
# {1: {1: 0.1, 2: 0.3}, 2: {1: 0.1, 2: 0.3}}
print p[2], p[2][2]
# {1: 0.1, 2: 0.3} 0.3
def dic_extend():
d1 = {'a': 1}
d2 = {'b': 2}
# add d2 to d1
d1.update(d2)
print d1 # {'a': 1, 'b': 2}
# reset d1
d1 = {'a': 1}
print dict(d1, **d2) # {'a': 1, 'b': 2}
def get_default_value():
d = {1: "xy1", 2: "xy2"}
# print d[3] # KeyError: 3
print d.get(3) # None
print d.get(3, "xy3") # xy3
if __name__ == '__main__':
get_default_value()
# dic_extend()
# dic_val_lst()
# dict_pop()
# sum_dic_val()
# count_val_lst_len()
# dic_count_value()
# dic_to_lst()
# dict_sort_by_value()
# lst_2_dict()
# iter_dict()
# iter_dic_sort()
# iter_dict_remove()
s_lst={}
s_lst['worker_1'] = [1, 2, 3]
s_lst['worker_2'] = [1, 2, 3]
# s_lst['worker_2'] = [1, 2, 3]
# print s_lst
# s_lst.pop('worker_1')
# print s_lst
# d_info = {}
# if d_info is None:
# print 'none'
# else:
# # 0
# print len(d_info)
# lst = [1, 2, 3]
pass