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', ...
随机推荐
- 异步三部曲之promise
概述 这是我看你不知道的JavaScript(中卷)的读书笔记,供以后开发时参考,相信对其他人也有用. 例子 首先来看一个例子,如果我们要异步获取x和y,然后把他们打印出来,那么用回调可以编写代码如下 ...
- Asp.net管线事件(页面请求周期)
在处理该请求时将由 HttpApplication 类执行以下事件. 希望扩展 HttpApplication 类的开发人员尤其需要注意这些事件. . 对请求进行验证,将检查浏览器发送的信息,并确定其 ...
- [LeetCode]无重复字符的最长子串
给定一个字符串,找出不含有重复字符的最长子串的长度. 示例 1: 输入: "abcabcbb" 输出: 3 解释: 无重复字符的最长子串是 "abc",其长度为 ...
- 课程二(Improving Deep Neural Networks: Hyperparameter tuning, Regularization and Optimization),第一周(Practical aspects of Deep Learning) —— 4.Programming assignments:Gradient Checking
Gradient Checking Welcome to this week's third programming assignment! You will be implementing grad ...
- Configuration problem: Failed to import bean definitions from relative location
问题现象: 最近开始做新需求,然后在Tomcat上部署项目时,出现了如下报错: [12-05 09:54:27,161 ERROR] ContextLoader.java:351 - Context ...
- ES6箭头函数this指向
普通函数中的this: 1. this总是代表它的直接调用者(js的this是执行上下文), 例如 obj.func ,那么func中的this就是obj 2.在默认情况(非严格模式下,未使用 'us ...
- Sublime Text3 一些实用设置
字体大小 "font_size": 14 高亮编辑中的那一行 "highlight_line": true 当你把脑袋扭过到显示器以外的地方后再回头看编辑器,光 ...
- Spring Security和JWT实现登录授权认证
目标 1.Token鉴权 2.Restful API 3.Spring Security+JWT 开始 自行新建Spring Boot工程 引入相关依赖 <dependency> < ...
- google 被墙的解决办法
昨晚无意中发现的东西,分享给各位使用,google搜索技术方面的东西还是很准确的,可惜被墙了,但是上有政策下有对策…… 谷歌地址: http://74.125.224.18/ http://91.21 ...
- Java转义emoji等特殊符号
写在前面 网上找了很多转emoji等方法,大多有两种方法 更改数据库编码格式为utf8mb4 过滤字符串中的emoji 都不是很优雅 更改数据库编码,势必影响其他数据库 过滤emoj效率比较低 处理E ...