python基础训练题2-元组,字典
1,判断值在元组中
>>> a = ( 1, 2, 3, 4, 10 )
>>> 10 in a
True
>>> '' in a
False
2,修改元组中的值,由于元组不能被直接修改,可以先把他转成列表,在通过列表修改之后,赋给一个新的元组对象
>>> a = ( 10, 20, 30, 40 )
>>> l = list( a )
>>> l[0] = 100
>>> t = tuple( l )
>>> t
(100, 20, 30, 40)
>>> id( a )
139920488716168
>>> id( t )
139920488447152
>>> type( a )
<type 'tuple'>
>>> type( t )
<type 'tuple'>
>>>
3,向集合添加一个值,删除一个值,求交集和并集
>>> a
set(['a', 'c', 'b', 'e', 'd', 'g', 'f'])
>>> a.add( 'ghostwu' )
>>> a
set(['a', 'c', 'b', 'e', 'd', 'g', 'f', 'ghostwu'])
>>> a.remove( 'g' )
>>> a
set(['a', 'c', 'b', 'e', 'd', 'f', 'ghostwu'])
>>> b = set( "abcdlmn" )
>>> a & b
set(['a', 'c', 'b', 'd'])
>>> a | b
set(['a', 'c', 'b', 'e', 'd', 'f', 'm', 'l', 'n', 'ghostwu'])
>>>
4,用字典实现一个学生成绩小系统,之后进行添加,修改,删除,排序等操作
>>> student = { 'ghostwu' : { 'name' : 'ghostwu', 'age' : 20, 'score' : { 'math' : 78, 'english' : 66, 'python' : 75 } } }
>>> student
{'ghostwu': {'age': 20, 'score': {'python': 75, 'math': 78, 'english': 66}, 'name': 'ghostwu'}}
>>> student['tom'] = { 'name' : 'tom', 'age' : 21, 'score' : { 'math' : 60, 'english' : 80, 'python' : 90 } }
>>> student
{'ghostwu': {'age': 20, 'score': {'python': 75, 'math': 78, 'english': 66}, 'name': 'ghostwu'}, 'tom': {'age': 21, 'score': {'python': 90, 'math': 60, 'english': 80}, 'name': 'tom'}}
>>> student['ghostwu']['score']['php'] = 90
>>> student['tom']['score']['php'] = 50
>>> student['ghostwu']['score']['math'] = 30
>>> del student['ghostwu']['age']
>>> score1 = student['ghostwu']['score'].values()
>>> score1
[75, 90, 30, 66]
>>> score1.sort()
>>> score1
[30, 66, 75, 90]
>>> student.pop( 'address', 'shenzhen' )
'shenzhen'
>>> student
{'ghostwu': {'score': {'python': 75, 'php': 90, 'math': 30, 'english': 66}, 'name': 'ghostwu'}, 'tom': {'age': 21, 'score': {'python': 90, 'php': 50, 'math': 60, 'english': 80}, 'name': 'tom'}}
>>>
python基础训练题2-元组,字典的更多相关文章
- Python 基础-python-列表-元组-字典-集合
列表格式:name = []name = [name1, name2, name3, name4, name5] #针对列表的操作 name.index("name1")#查询指定 ...
- Python初学笔记列表&元组&字典
一.从键盘获取 1 print("请输入") 2 username = input("姓名:") 3 age = input("年龄:") ...
- Python基础训练题-简单数学公式
1.在100内,将遇到被7除余数为0的都显示PASS: n=1 while n < 101: if n % 7 == 0: pass print('pass') else: print(n) n ...
- python基础训练题1-列表操作
1,在列表末尾添加一个值 >>> l = [ 10, 20 ] >>> l [10, 20] >>> l.append( 'ghostwu' ) ...
- python学习笔记(一)元组,序列,字典
python学习笔记(一)元组,序列,字典
- Python第三天 序列 数据类型 数值 字符串 列表 元组 字典
Python第三天 序列 数据类型 数值 字符串 列表 元组 字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...
- python中列表 元组 字典 集合的区别
列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计 ...
- Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式
Python第三天 序列 5种数据类型 数值 字符串 列表 元组 字典 各种数据类型的的xx重写xx表达式 目录 Pycharm使用技巧(转载) Python第一天 安装 shell ...
- Python学习三|列表、字典、元组、集合的特点以及类的一些定义
此表借鉴于他人 定义 使用方法 列表 可以包含不同类型的对象,可以增减元素,可以跟其他的列表结合或者把一个列表拆分,用[]来定义的 eg:aList=[123,'abc',4.56,['inner', ...
随机推荐
- pyqt5 Button.click 报错:argument 1 has unexpected type 'NoneType'
如上所示,在括号中,添加‘lambda:’,就可以成功运行,不知道为啥. 参考:https://blog.csdn.net/flhsxyz/article/details/79220936?utm_s ...
- Linux下查看文档指令
1,cat直接查看 cat /etc/profile 2,more,less分页查看 less /etc/profile more /etc/profile less -N /etc/profile ...
- Iframe高度自适应(兼容IE/Firefox、同域/跨域)
在实际的项目进行中,很多地方可能由于历史原因不得不去使用iframe,包括目前正火热的应用开发也是如此. 随之而来的就是在实际使用iframe中,会遇到iframe高度的问题,由于被嵌套的页面长度不固 ...
- Git 本地操作
版权声明:数学是研究世界的本质,自然科学是研究上帝的意志,而计算机则是揣摩屌丝人类的意志 目录(?)[-] 命令 git config 增删改查 init clone add commit sta ...
- iOS开发-从16进制颜色中获取UIColor
目前iOS中设置UIColor只能使用其枚举值.RGB等方法,不能直接将常用的16进制颜色值直接转为UIColor对象,所以写了点代码,将16进制颜色值转为UIColor. 代码如下, //头文件#i ...
- js缓存问题,修改js后代码不生效
问题描述 最近在上线新版本项目的时候,发现有的用户的操作还是调用的老版本JS里面的内容,这样就造成原来新的JS里面加上的限制不能限制用户的操作,从而导致用户可以重复操作. 问题产生原因 如果在用户之前 ...
- django admin编辑被外键关联的主表时支持显示字表记录
假设有模型 class A(models.Model): name = models.CharField() class B(models.Model): name = models.CharFiel ...
- 在Ubuntu 16.04上利用Jexus+.Net Core+Linux版SQL Server部署ZKEACMS.Core
百度传课视频地址:https://chuanke.baidu.com/v5849090-223278-1498090.html 一.SQL Server on Linux的安装 官方文档:https: ...
- 深入了解preventDefault与stopPropagation
event.preventDefault()用法介绍(阻止默认事件) 该方法将通知 Web 浏览器不要执行与事件关联的默认动作(如果存在这样的动作).例如,如果 type 属性是 "subm ...
- JS中this的那些事儿
this是JavaScript中最复杂的机制之一.它是一个很特别的关键字,被自动定义在所有函数的作用域中. 一.this到底指向什么? this既不指向函数自身,也不指向函数的词法作用域,具体指向什么 ...