Python之Dict和Set类型(入门5)
转载请标明出处:
http://www.cnblogs.com/why168888/p/6407905.html本文出自:【Edwin博客园】
Python之Dict和Set类型
1. Python之什么是dict
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59,
'Paul': 75
}
2. Python之访问dict
d = {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
print 'Adam:', d['Adam']
print 'Lisa:', d['Lisa']
print 'Bart:', d['Bart']
3. Python中dict的特点
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
4. Python更新dict
d = {
95: 'Adam',
85: 'Lisa',
59: 'Bart'
}
d[72] = 'Paul'
5. Python之 遍历dict
d= {
'Adam': 95,
'Lisa': 85,
'Bart': 59
}
for key in d:
print key + ':', d[key]
6. Python中什么是set
s = set(['Adam', 'Lisa', 'Bart', 'Paul'])
7. Python之 访问set
s = set(['Adam', 'adam', 'Lisa', 'lisa', 'Bart', 'bart', 'Paul', 'paul'])
print 'adam' in s
print 'bart' in s
8. Python之 set的特点
months = set(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'])
x1 = 'Feb'
x2 = 'Sun'
if x1 in months:
print 'x1: ok'
else:
print 'x1: error'
if x2 in months:
print 'x2: ok'
else:
print 'x2: error'
9. Python之 遍历set
s = set([('Adam', 95), ('Lisa', 85), ('Bart', 59)])
for x in s:
print x[0] + ':', x[1]
10. Python之 更新set
s = set(['Adam', 'Lisa', 'Paul'])
L = ['Adam', 'Lisa', 'Bart', 'Paul']
for name in L:
if name in s:
s.remove(name)
else:
s.add(name)
print s
Python之Dict和Set类型(入门5)的更多相关文章
- Python 基础 Dict 和 Set 类型
python 什么是dict 例如: d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } 我们把名称称为key,对应的成绩称为value,dic就是通过key 来查找 ...
- Python之List和Tuple类型(入门3)
转载请标明出处: http://www.cnblogs.com/why168888/p/6407682.html 本文出自:[Edwin博客园] Python之List和Tuple类型 1. Pyth ...
- python之Dict和set类型
Dict就是一种key:value的表格: >>> d = { 'Adam':95, 'Lisa':85, 'Bart':59, 'Paul':75 } >>> p ...
- Python 入门(六)Dict和Set类型
什么是dict 我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字: ['Adam', 'Lisa', 'Bart'] 或者考试的成绩列表: [95, 85, 59] ...
- python 学习之dict和set类型
什么是dict 我们已经知道,list 和 tuple 可以用来表示顺序集合,例如,班里同学的名字: ['Adam', 'Lisa', 'Bart'] 或者考试的成绩列表: [95, 85, 59] ...
- python 字典dict - python基础入门(15)
前面的课程讲解了字符串str/列表list/元组tuple,还有最后一种比较重要的数据类型也需要介绍介绍,那就是python字典,俗称:dict. python中的字典可与字符串/列表/元组不同,因为 ...
- Python:Base2(List和Tuple类型, 条件判断和循环,Dict和Set类型)
1.Python创建list: Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 比如,列出班里所有同学的名字,就可以用一个list表示: &g ...
- Python中dict的特点、更新dict、遍历dict
dict的第一个特点是查找速度快,无论dict有10个元素还是10万个元素,查找速度都一样.而list的查找速度随着元素增加而逐渐下降. 不过dict的查找速度快不是没有代价的,dict的缺点是占用内 ...
- python 字典 dict 该注意的一些操作
在用python处理dict 的时候,有几个该注意的地方,这里跟大家提一下: 1)操作dict 时,尽量少产生新的列表对象.比如: 遍历dict的时候,如果用 dic = {"a" ...
随机推荐
- Node.js des加密
常见的加密算法基本分为这几类,1 :线性散列算法.2:对称性加密算法.3.非对称性加密算法 (记记记) 线性散列算法(签名算法):MD5,SHA1,HMAC比如MD5:即Message-Digest ...
- window.location.href.substr(window.location.href.length - 6)
if (window.location.href.substr(window.location.href.length - 6) == "flag=1") { var tOptio ...
- bootstrap-select在angular上的应用
1.bootstrap-select 依赖bootstrap.js ,又依赖jQuery,这些都可以用requirejs来处理. 2.一般bootstrap-select 都放在具体的模块上,而是动态 ...
- ASP.NET Web API根据代码注释生成Help文档
使用Visual Studio新建一个ASP.NET Web API项目,直接运行,查看Help文档可以看到如下的API帮助说明 如何在Description中显示描述. 1. 打开Controlle ...
- 修改django 后台admin用户的密码
python manage.py shellfrom django.contrib.auth.models import User from django.contrib.auth.models im ...
- mac os下载安装jmeter
一.简介 jmeter是属于apache的一个开源产品,纯Java应用.最初用来进行功能测试,而后又扩展了更多的测试功能. 二.下载 进入apache的jmeter下载页:http://jmeter. ...
- 八、profile多环境配置
通常我们的程序有着多个环境: 1.开发环境: 2.生产环境. 等 环境的配置各不相同,我们希望通过一个简单的配置来切换环境,而springboot轻松地实现了该功能: 一.多环境需要多配置文件 一般我 ...
- UVA 10328(DP,大数,至少连续)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=19825 这道题和http://www.cnblogs.com/qlky/p/ ...
- elastic job 动态设置定时任务
1. 版本 <!-- import elastic-job lite core --> <dependency> <groupId>com.dangdang< ...
- 盒模型的属性丶display显示丶浮动
一丶盒模型的属性(重要) 1.padding padding是标准文档流,父子之间调整位置 <!DOCTYPE html> <html> <head> <me ...