字典 dict方法
字典
student = {'sId': '1101', 'sName': '张三', 'sClass': '软件测试', 'sColl': '信息技术学院'}
# 根据键查询 若不存在会报错
print(student['sId'])
# get查询 若不存在返回None
print(student.get('sId'))
print(student.get('sId1'))
# 字典长度,元素个数
print(len(student))
# 字典字符串显示 print本身就是将输出内容转成字符串输出 所以以下输出一致
print(str(student))
print(student)
# 以列表返回字典中所有的键
print(student.keys())
# 返回一个字典中所有的值
print(student.values())
# 返回所有键值对
print(student.items())
# 判断键是否在字典中 严格区分大小写
print('sid' in student)
# for循环遍历的几种方式
for key in student:
print(key, end='\t')
print()
for key in student.keys():
print(key, end='\t')
print()
for key, value in student.items():
print('%s,%s' % (key, value), end='\t')
print()
# 复制字典
student.copy()
# 创建一个新字典,以序列 seq 中元素做字典的键,val 为字典所有键对应的初始值
student1 = {}
student1 = student1.fromkeys((1,), 'num')
print(str(student1))
# 存在无效,不存在增加
student.setdefault('sId', '1002')
student.setdefault('sSex', '男')
print(student)
# 把另一个字典的键/值更新到一个字典中
student.update(student1)
print(student)
# 字典生成式
list2dict=[{'name','zhagnsan'},{'age',22},{'phone',110}]
#for循环遍历列表,将列表中每个二元集合的一号和二号取出,作为字典的key:value
dict{key:value for key,value in list2dict}
print(dict)
字典 dict方法的更多相关文章
- python中几个常见的黑盒子之“字典dict” 与 “集合set”
这里说到"字典dict" 和 "集合set"类型,首先,先了解一下,对于python来说,标准散列机制是有hash函数提供的,对于调用一个__hash__方法: ...
- Python中的元组(tuple)、列表(list)、字典(dict)
-------------------------------更新中-------------------------------------- 元组(tuple): 元组常用小括号表示,即:(),元 ...
- python中的字典(dict),列表(list),元组(tuple)
一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...
- Python - 字典(dict) 详解 及 代码
字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...
- python基础之字典dict和集合set
作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...
- 自学Python2.4-基本数据类型-字典dict(objct)
Python dict方法总结 一.字典介绍 1.字典概述 ①字典是python中唯一内建的映射类型.又称关联数组或散列②映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关 ...
- Python字典(dict)使用技巧
字典dict是Python中使用频率非常高的数据结构,关于它的使用,也有许多的小技巧,掌握这些小技巧会让你高效地的使用dict,也会让你的代码更简洁. 1.默认值 假设name_for_userid存 ...
- python基础2--数据结构(列表List、元组Tuple、字典Dict)
1.Print函数中文编码问题 print中的编码:# -*- coding: utf-8 -*- 注:此处的#代表的是配置信息 print中的换行符,与C语言相同,为"\n" 2 ...
- Python3 字典 items() 方法
描述 Python 字典 items() 方法以列表返回可遍历的(键, 值) 元组数组. 语法 items()方法语法: dict.items() 参数 NA. 返回值 返回可遍历的(键, 值) 元组 ...
随机推荐
- [TimLinux] Python 再谈元类 metaclass
本博文通过对以下链接进行理解后,编写. https://stackoverflow.com/questions/100003/what-are-metaclasses-in-python 1. 类 类 ...
- [TimLinux] JavaScript input框的onfocus/onblur/oninput/onchange事件介绍
1. onfocus事件 input框获取到焦点时,触发了该事件,比如获取到焦点时,修改input框的背景色.这个功能其实可以使用css的伪类:focus来定义. 2. onblur事件 这个与onf ...
- poj3111 K Best 最大化平均值,二分
题目:http://poj.org/problem?id=3111 题意:给你n,k,n个数的v.w值,选出k个数,使得v之和/w之和最大化. 思路:一看到题目,这不是赤果果的贪心吗?为什么放在二分专 ...
- HDU5973 Game of Geting Stone(威佐夫博弈)
Two people face two piles of stones and make a game. They take turns to take stones. As game rules, ...
- Day 07 字符串内置方法和爬虫基础3
目录 异常处理 LeetCode使用之两数之和(示例) 字符串内置方法 爬虫基础3 selenium基本使用 selenium模拟百度登录 selenium爬取京东商品信息 异常处理 try: pri ...
- 【Babel】293- 初学 Babel 工作原理
戳蓝字「前端技术优选」关注我们哦! 前言 babel Babel 对于前端开发者来说应该是很熟悉了,日常开发中基本上是离不开它的. 已经9102了,我们已经能够熟练地使用 es2015+ 的语法.但是 ...
- webpack 环境搭建
Webpack环境搭建 一.安装node 1.node官网下载node并安装----node里面内置了npm所以用在安装npm了 2.命令行输入node -v查看node是否安装成功 二.全局安装we ...
- 分布式事务解决方案,中间件 Seata 的设计原理详解
作者:张乘辉 前言 在微服务架构体系下,我们可以按照业务模块分层设计,单独部署,减轻了服务部署压力,也解耦了业务的耦合,避免了应用逐渐变成一个庞然怪物,从而可以轻松扩展,在某些服务出现故障时也不会影响 ...
- hdu 4725 The Shortest Path in Nya Graph (最短路+建图)
The Shortest Path in Nya Graph Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- dev gridcontrol设置过滤器下拉列表
调用: //为类别名称列启用选中的过滤器下拉式样式. ].OptionsFilter.FilterPopupMode = FilterPopupMode.CheckedList; //订阅ShowFi ...