字典

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

  1. python中几个常见的黑盒子之“字典dict” 与 “集合set”

    这里说到"字典dict" 和 "集合set"类型,首先,先了解一下,对于python来说,标准散列机制是有hash函数提供的,对于调用一个__hash__方法: ...

  2. Python中的元组(tuple)、列表(list)、字典(dict)

    -------------------------------更新中-------------------------------------- 元组(tuple): 元组常用小括号表示,即:(),元 ...

  3. python中的字典(dict),列表(list),元组(tuple)

    一,List:列表 python内置的一种数据类型是列表:list.list是一种有序的数据集合,可以随意的添加和删除其中的数据.比如列出班里所有的同学的名字,列出所有工厂员工的工号等都是可以用到列表 ...

  4. Python - 字典(dict) 详解 及 代码

    字典(dict) 详解 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/17291329 字典(dict)是表示映射的数据 ...

  5. python基础之字典dict和集合set

    作者:tongqingliu 转载请注明出处:http://www.cnblogs.com/liutongqing/p/7043642.html python基础之字典dict和集合set 字典dic ...

  6. 自学Python2.4-基本数据类型-字典dict(objct)

    Python dict方法总结 一.字典介绍 1.字典概述 ①字典是python中唯一内建的映射类型.又称关联数组或散列②映射类型对象里哈希值(键,key)和指向的对象(值,value)是一对多的的关 ...

  7. Python字典(dict)使用技巧

    字典dict是Python中使用频率非常高的数据结构,关于它的使用,也有许多的小技巧,掌握这些小技巧会让你高效地的使用dict,也会让你的代码更简洁. 1.默认值 假设name_for_userid存 ...

  8. python基础2--数据结构(列表List、元组Tuple、字典Dict)

    1.Print函数中文编码问题 print中的编码:# -*- coding: utf-8 -*- 注:此处的#代表的是配置信息 print中的换行符,与C语言相同,为"\n" 2 ...

  9. Python3 字典 items() 方法

    描述 Python 字典 items() 方法以列表返回可遍历的(键, 值) 元组数组. 语法 items()方法语法: dict.items() 参数 NA. 返回值 返回可遍历的(键, 值) 元组 ...

随机推荐

  1. [TimLinux] Python 使用入门

    1. 为什么用Python 软件质量:Python注重可读性.一致性和软件质量. 提高开发者的效率:Python代码的大小往只有C++/Java代码的1/5 ~ 1/3. 程序的可移植性:绝大多数Py ...

  2. POJ3261 Milks patterns(后缀数组)

    Farmer John has noticed that the quality of milk given by his cows varies from day to day. On furthe ...

  3. python爬虫学习---爬取微软必应翻译(中英互译)

    前言本文的文字及图片来源于网络,仅供学习.交流使用,不具有任何商业用途,版权归原作者所有,如有问题请及时联系我们以作处理.作者:OSinooO 本人属于python新手,刚学习的 python爬虫基础 ...

  4. iSensor App Kit 测试之 MT9V111 MT9M111 MT9D111

    iSensor App Kit 可以调试测试一切常规的sensor,对于ccusb20底板,可以直接兼容官哥所有的dvp接口的摄像头,分辨率从30w到1400w均没问题. 今天又测试了三款sensor ...

  5. [Cake] 3. dotnet 本地工具 cake & dotnet format

    在上一篇[Cake] 2. dotnet 全局工具 cake中介绍了通过.Net Core 2.1 的全局工具dotnet tool命令来简化cake的安装和使用.因为是全局安装,则无法适应每个项目对 ...

  6. ruby 使用 rqrcode 生成二维码

    参考:  https://github.com/whomwah/rqrcode 1.  gem 'rqrcode' 2.  在helper中: require 'base64' def generat ...

  7. mysql5.x数据库文件导入mysql5.7数据库出错解决方法

    由于mysql5.7 timestamp 类型不允许timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',创建表的是后会报异常 修改mysql5.7的配置文 ...

  8. Spring源码加载BeanDefinition过程

    本文主要讲解Spring加载xml配置文件的方式,跟踪加载BeanDefinition的全过程. 源码分析 源码的入口 ClassPathXmlApplicationContext构造函数 new C ...

  9. JS基础-变量类型和类型转换

    JS 变量类型 JS中有 6 种原始值,分别是: boolean number string undefined symbol null 引用类型: 对象 数组 函数 JS中使用typeof能得到哪些 ...

  10. 【设计模式】代理模式-Proxy

    转载:https://www.cnblogs.com/yangchongxing/p/7654725.html 代理模式定义如下: Provide a surrogate or placeholder ...