python 常用技巧 — 字典 (dictionary)
目录:
1. python 相加字典所有的键值 (python sum all values in dictionary)
2. python 两个列表分别组成字典的键和值 (python two list serve as key and value for dictionary)
3. python 把字典所有的键值组合到一个列表中 (python get dictionary values to combine a list)
内容:
1. python 相加字典所有的键值 (python sum all values in dictionary)
https://stackoverflow.com/questions/4880960/how-to-sum-all-the-values-in-a-dictionary
d = {'a':1, 'b':2, 'c':4}
sum(d.values())
7
2. python 两个列表分别组成字典的键和值 (python two list serve as key and value for dictionary)
https://stackoverflow.com/questions/209840/convert-two-lists-into-a-dictionary
In [1]: layout_type = ['LTTextBox', 'LTFigure', 'LTImage', 'LTLine', 'LTRect'] In [2]: color = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (160, 32, 240)] In [3]: draw_color = dict(zip(layout_type, color)) In [4]: draw_color
Out[4]:
{'LTFigure': (0, 255, 0), 'LTImage': (0, 0, 255), 'LTLine': (255, 255, 0), 'LTRect': (160, 32, 240), 'LTTextBox': (255, 0, 0)}
3. python 把字典所有的键值组合到一个列表中 (python get dictionary values to combine a list)
https://thispointer.com/python-how-to-create-a-list-of-all-the-values-in-a-dictionary/
In [205]: a = {'apple':2, 'banana':3, 'pear':5} In [206]: a
Out[206]: {'apple': 2, 'banana': 3, 'pear': 5} In [207]: list(a.values())
Out[207]: [2, 3, 5]
4.
python 常用技巧 — 字典 (dictionary)的更多相关文章
- python数据类型:字典Dictionary
python数据类型:字典Dictionary 字典是一种可变容器模型,可以存储任意类型对象 键是唯一的,但是值不需要唯一 值可以取任何数据类型,但是键必须是不可变的,如字符串,数字,元组 创建字典: ...
- python学习之字典(Dictionary)练习
Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型 字典中分为键值对 , key 类型需要时被哈希. value 类型可以是 字符串.数字.元组等其他容器模 ...
- Python常用数据结构-字典——2.1 字典方法 keys()
python字典常用方法: keys() # 获取所有的键 values() # 获取所有的值 items() # 获 ...
- python常用技巧
1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中: 通过fi ...
- python 常用技巧
一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...
- python常用技巧 — 杂
目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...
- python 常用技巧 — 列表(list)
目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...
- python 常用技巧 — 数组 (array)
目录: 1. 数组每一行除以这一行的总数(numpy divide row by row sum) 2. 数组每一行或者每一列求平均 (python average array columns or ...
- #1 Python灵活技巧
前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...
随机推荐
- Hibernate入门核心配置文件和orm元数据配置文件详解
框架是什么? 框架是用来提高开发效率的 封装了一些功能,我们需要使用这些功能时,调用即可,不用手动实现 所以框架可以理解为一个半成品的项目,只要懂得如何使用这些功能即可 Hibernate是完全面向对 ...
- css linear-gradient;心跳animation
css线性背景 background:linear-gradient(20deg,#ccffff,#ffcccc); transform transform:scale(1.5); transform ...
- 每天一个linux命令:which(17)
which which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录.which指令会在环境变量$PATH设置的目录里查找符合条件的文件.也就是说,使用whic ...
- loadRunner函数之web_add_header
web_add_header 功能:用于添加指定的报文头到下一次HTTP请求 格式:web_add_header( const char *Header, const char *Content ), ...
- delphi+mysql做的图书管理系统,怎么把mysql数据库也一起打包进去?我用的是delphi的Express组件。
sqlconnection,sqlquery1这些组件,我连接数据库的时候是用对象编辑器里的属性进行连接的,在sqlconnection中指定了字符集utf8,有些人做的方法是利用代码连接的数据库,如 ...
- LOJ 3094 「BJOI2019」删数——角标偏移的线段树
题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...
- python实现计时器(装饰器)
1.写一个装饰器,查看函数执行的时间 import time # 装饰器run_time,@run_time加在谁头上,谁就是参数fundef run_time(fun): start_time = ...
- (转)Windows下安装Docker, GitBash环境配置
转:https://blog.csdn.net/chengly0129/article/details/68944269 官网介绍: https://docs.docker.com/toolbox/t ...
- 使用DataV制作实时销售数据可视化大屏(实验篇)
课时1:背景介绍 任务说明 ABC是一家销售公司,其客户可以通过网站下单订购该公司经营范围内的商品,并使用信用卡.银行卡.转账等方式付费.付费成功后,ABC公司会根据客户地址依据就近原则选择自己的货仓 ...
- docker 部署netcore 的关键语句
网站容器:docker run -it --name myTestWeb -p 8080:80 -v /mnt/hgfs/my_share/core/website/:/website microso ...