-
Notifications
You must be signed in to change notification settings - Fork 0
/
dict_tools.py
59 lines (54 loc) · 1.45 KB
/
dict_tools.py
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
# -*- coding:utf-8 -*-
'''
本模块提供与字典相关的操作
'''
import re
import os
import sys
import csv
import xlrd
import codecs
import random
import chardet
import numpy as np
from copy import deepcopy
try:
import cPickle as pickle
except:
import pickle
def find_max_n_in_dict(ori_dict, N):
'''
本函数用于在字典中选取最大的N个
[ori_dict](dict): 原字典
[N](int): 最大的个数
'''
i = 0
new_dict = {}
ori_dict = dict(ori_dict)
for i in range(0,N):
max_key = max(ori_dict, key=lambda x: ori_dict[x])
new_dict[max_key] = ori_dict.pop(max_key)
return new_dict
def find_min_n_in_dict(ori_dict, N):
'''
本函数用于在字典中选取最小的N个
[ori_dict](dict): 原字典
[N](int): 最小的个数
'''
i = 0
new_dict = {}
ori_dict = dict(ori_dict)
for i in range(0,N):
min_key = min(ori_dict, key=lambda x: ori_dict[x])
new_dict[min_key] = ori_dict.pop(min_key)
return new_dict
def list_to_dict(ori_list, key_place=0):
'''
本函数用于将二级列表以其最低级列表的指定位置为键,其他位置为值生成字典
[ori_list](list): 输入列表
[key_place](int): 作为键的位置
'''
tar_dict = {}
for line in ori_list:
tar_dict[line[key_place]] = [element for element in (line[:key_place]+line[key_place+1:])]
return tar_dict