目录:

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)的更多相关文章

  1. python数据类型:字典Dictionary

    python数据类型:字典Dictionary 字典是一种可变容器模型,可以存储任意类型对象 键是唯一的,但是值不需要唯一 值可以取任何数据类型,但是键必须是不可变的,如字符串,数字,元组 创建字典: ...

  2. python学习之字典(Dictionary)练习

    Python字典是另一种可变容器模型,且可存储任意类型对象,如字符串.数字.元组等其他容器模型 字典中分为键值对 , key 类型需要时被哈希. value 类型可以是 字符串.数字.元组等其他容器模 ...

  3. Python常用数据结构-字典——2.1 字典方法 keys()

    python字典常用方法: keys()               #  获取所有的键 values()            #  获取所有的值 items()              #  获 ...

  4. python常用技巧

    1,关于tab键与4个空格: 由于不同平台间,tab键值设置有所区别,据相关介绍,官方在缩进方面推荐使用4个空格.方便起见,可设置tab自动转换为4个空格. 1.1在pycharm中:    通过fi ...

  5. python 常用技巧

    一.字符串与数值的转换 Python中字符串转换为数值: str_num = '99' num = int(str_num) 整型数转换为字符串: num = 99 str_num = str(num ...

  6. python常用技巧 — 杂

    目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...

  7. python 常用技巧 — 列表(list)

    目录: 1. 嵌套列表对应位置元素相加 (add the corresponding elements of nested list) 2. 多个列表对应位置相加(add the correspond ...

  8. python 常用技巧 — 数组 (array)

    目录: 1. 数组每一行除以这一行的总数(numpy divide row by row sum) 2. 数组每一行或者每一列求平均 (python average array columns or ...

  9. #1 Python灵活技巧

    前言 Python基础系列博文已顺利结束,从这一篇开始将进入探索更加高级的Python用法,Python进阶系列文章将包含面向对象.网络编程.GUI编程.线程和进程.连接数据库等.不过在进阶之前,先来 ...

随机推荐

  1. Ubuntu12.04安装配置x11vnc

    全程在root下进行 安装x11vnc sudo apt-get install vino vinagre x11vnc 设置密码 sudo x11vnc -storepasswd sudo x11v ...

  2. centos7在线安装mysql8.0.16

    一.官网复制安装源地址: 1.进入官网地址:https://dev.mysql.com/downloads/repo/yum/ 二.进入/usr/local目录下 ,创建mysql文件夹 三.使用命令 ...

  3. C++ 彩色图像(RGB)三通道直方图计算和绘制,图像逆时针旋转90° 实现代码

    #include "iostream" #include "opencv2/opencv.hpp" #include "vector" us ...

  4. 【HDOJ6609】Find the answer(线段树)

    题意:给定一个n个正整数的数列,第i项为w[i],对于每个i,你要从[1,i-1]中选择一些变成0,使得变化后[1,i]的总和小于m,每次询问最少要变几个 n<=2e5,m<=1e9,1& ...

  5. 运行python不报错,运行pip报错

    Fatal error in launcher: Unable to create process using '""c:\program files (x86)\python36 ...

  6. Transform.Find()

    代码演示: using System.Collections;using System.Collections.Generic;using UnityEngine; public class Tran ...

  7. 读取 appsettings.json

    Appsettings.json 配置: 个配置文件就是一个json文件,并且是严格的json文件,所有的属性都需要添加“”引号.下图是一个常规的代码示例: {"UrlString" ...

  8. Android深度探索-卷1第六章心得体会

    这章主要介绍了第一个linux驱动程序:统计单词个数.Linux系统将每一个驱动都映射成一个文件,这些文件称为设备文件或驱动文件,都保存在/dev目录中.大多数Linux驱动都有与其对应的设备文件,因 ...

  9. idea新手教程 如何springmvc创建Maven项目

    1.点击Create project,选择maven,勾选Create from archetype选择web-app,如图 2 输入    Group-Id     //组织名,也是作者名 Arti ...

  10. Add a system call on Ubuntu 13.04(x64) with x86_64

    We added a system call to modify idt table, then programed it in modify_idt.c 1. Put our modify_idt. ...