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编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...
随机推荐
- python基础:3.高级运算符
1.异或运算 十进制的异或运算,先转成二进制进行异或,按位进行比较,对应位置相同则为0,对应位置不同则为1,,再从异或结果转成十进制. python中: 1 ^ 1 = 0 1 ^ 2 = 3 1 ^ ...
- 用pycharm运行pytest
安装pytest 1. 在pycharm中建项目,建文件,文件名字要以test_开头 2.在文件中插入pytest模块 import pytest #引用pytest模块 3.定义test函数,以及断 ...
- leetcode-15双周赛-1288-删除被覆盖区间
题目描述: 方法一:排序O(Nlogn) class Solution: def removeCoveredIntervals(self, intervals: List[List[int]]) -& ...
- boost asio scalability and multithreading
A library such as Boost.Asio is typically used to achieve greater efficiency. With no need to wait f ...
- kafka-server.properties
# Licensed to the Apache Software Foundation (ASF) under one or more # contributor license agreement ...
- puppet(一种Linux、Unix、windows平台的集中配置管理系统)
puppet是一种Linux.Unix.windows平台的集中配置管理系统,使用自有的puppet描述语言,可管理配置文件.用户.cron任务.软件包.系统服务等.puppet把这些系统实体称之为资 ...
- DZY Loves Chemistry
DZY Loves Chemistry time limit per test 1 second memory limit per test 256 megabytes input standard ...
- react教程 — 性能优化
参考:https://segmentfault.com/a/1190000007811296?utm_medium=referral&utm_source=tuicool 或 https: ...
- ArcMap如何撤销配准
ArcMap地理配准时,更新地理配准后,就没法撤销了. 如何解决呢,更新地理配准后,会在源文件夹中自动生成配准文件(文件格式为.over .jgwx .xml),可以通过删除这些文件来清除配准.
- django-redis-cache缓存使用
1. redis安装配置 (1)到redis目录 [root@localhost redis-2.8.17]# ls 00-RELEASENOTES CONTRIBUTING deps Makefil ...