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', ...
随机推荐
- JQuery Mobile - 解决页面点击时候,页眉和页脚消失问题!
当点击页面时候,页眉和页脚会消失!解决方法,在页面和页脚中加入: data-quicklinks="true" 实际使用代码: <div data-role="pa ...
- jzoj3084
發現題目函數本質是: 1.將某一數x的末尾1去掉 2.不斷將這個數/2,直到遇到新的1 我們發現一個數z可以用y步到達數x,記x二進制長度為c,分2種情況討論: 1.x是奇數,則z的前c個二進制數必須 ...
- 初探日志框架Logback
一. 背景 最近因为学习项目时需要使用logback日志框架来打印日志, 使用过程中碰到很多的疑惑, 而且需要在控制台打印mybatis执行的sql语句, 于是决定沉下心来 研究一下logback的使 ...
- 人工智能-机器学习之Selenium(chrome驱动,火狐驱动)
selenium是一个用于web应用程序测试的工具,Selenium测试直接运行在浏览器中,就像真正的用户在操作一样.支持的浏览器包括IE.Mozilla Firefox.Mozilla Suite等 ...
- POJ 2575
#include<iostream> #include<set> #include<stdio.h> using namespace std; int my_abs ...
- vue教程3-01 路由、组件、bower包管理器使用
vue教程3-01 路由.组件.包管理器 以下操作前提是 已经安装好node.js npm bower-> (前端)包管理器 下载: npm install bower -g 验证: bower ...
- 坑爹的Sun JDK
Sun的这个java.lang.Throwable 源码 设计非常糟糕,完全没有扩展性, 我在IBM 的Java JDK下,继承java.lang.Throwable重新定义了一个ExceptionW ...
- oracle exp imp日常使用
http://www.cnblogs.com/ningvsban/archive/2012/12/22/2829009.html http://www.cnblogs.com/mq0036/archi ...
- 字符、字符串和文本的处理之String类型
.Net Framework中处理字符和字符串的主要有以下这么几个类: (1).System.Char类 一基础字符串处理类 (2).System.String类 一处理不可变的字符串(一经创建,字符 ...
- iOS Round Double value to 2 decimal digits and round 3rd decimal digit up
I have I double value like this one 17.125. It should be rounded up to 17.13 If I use the simple met ...