python dict常用
来源:互联网 发布:模架编程培训要多久 编辑:程序博客网 时间:2024/06/05 09:12
本文主要是python中dict常用的方法:
list 转化为 dict
dict遍历删除指定条件的元素
dict安装key或者value排序
dict的value统计计数
两个list转化为dict
def lst_2_dict(): """ combine two list to dict :return: """ lst1 = ['a', 'b', 'c'] lst2 = [1, 2, 3] # d = {k: v for k, v in zip(lst1, lst2)} d = dict(zip(lst1, lst2)) print d # {'a': 1, 'c': 3, 'b': 2}
删除dict中value < 0 的元素
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
按照key的顺序变量dict
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]
安装key或者value对dict排序:
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)
对dict的value计数:
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}) # 统计none与非none的数量 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
统计dict中每个list的长度:
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
dict value求和:
def sum_dic_val(): d_info = {'33': 1, '88': 2, '22': 3, '44': 4} print sum(d_info.values()) # 10
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
具体见本人的github
参考:
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
阅读全文
0 0
- python dict常用
- python dict常用方法简介
- python set、list、dict常用操作
- python dict
- python dict
- python dict
- Python---dict
- python-dict
- python-dict
- Python dict
- Python dict
- Python-dict
- Python Dict
- 【Python】【Dict】dict 基本用法
- python中tuple/list/dict/set常用方法
- python---字典dict的常用方法和操作
- python中字典(dict)的常用操作
- Python 列表(list)、字典(dict)、字符串(string) 常用操作小结
- Decorator函数
- 【Java学习笔记】父类引用指向子类对象之属性
- NDK开发系列二
- Hadoop读书笔记(一)——Hadoop分布式文件系统
- CS231n A2: Global name 'col2im_6d_cython' is not defined解决(Mac)
- python dict常用
- R语言UTF-8各种问题解决方法
- TypeError: data type "datetime" not understood
- canvas (1) 画一些简单的图形
- UDT的连接建立和释放
- 1加到100的和
- 常见非关系型数据库(NoSQL)推荐介绍
- NetBeans 配置文件说明
- java学习笔记《java从入门到精通》