python基础训练题2-元组,字典

1,判断值在元组中

>>> a = ( 1, 2, 3, 4, 10 )
>>> 10 in a
True
>>> ‘10‘ 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‘}}
>>> 

原文地址:https://www.cnblogs.com/ghostwu/p/8672278.html

时间: 2024-07-31 02:05:11

python基础训练题2-元组,字典的相关文章

Python数据结构之列表元组字典的用法

数据结构的含义 在学习数据结构之前,我们先来了解下数据结构的含义.数据结构是通过某种方式(例如对元素进行编号)组织在一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在Python语言中,最基本的数据结构是序列(sequence).序列中的每个元素被分配一个序号----即元素的位置,称为索引或下标,索引从0开始递增. 典型的序列包括列表.元组和字符串.其中,列表是可变的(可修改),而元组和字符串是不可变的(一旦创建了就是固定的).列表.元组和字符串也是较常用的数据结构

[Python日记-2]列表-元组-字典-if-for

今天学习了列表,元组,字典相关知识,真的琐碎.我应该是学了好几遍了,刚开始是充满激情的,学到一个方法就迫不及待地去尝试,现在也平和了.好了,总结下. 1. 列表 Python中用方括号([])来表示列表,并用逗号来分隔其中的元素.要访问列表元素,列表的名称[索引]. 索引可以是负值,如将索引指定为-1,可让Python返回最后一个列表元素.可以在不明确列表长度的情况下,访问最后的元素. 1.1 列表中添加元素的方法: 1 Lis = [] 2 3 Lis.title() #使列表中每个元素的首字

python 数据类型 变量 列表 元组 字典 集合

Python中,能够直接处理的数据类型有以下几种: 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 计算机由于使用二进制,所以,有时候用十六进制表示整数比较方便,十六进制用0x前缀和0-9,a-f表示,例如:0xff00,0xa5b4c3d2,等等. 浮点数 浮点数也就是小数,之所以称为浮点数,是因为按照科学记数法表示时,一个浮点数的小数点位置是可变的,比如,1.23x109和12.3x108是完全相等

python的列表,元组和字典简单介绍

引 入 java                                   python 存取多个值:数组或list集合 ------------------------> 列表,元组 key-value格式:    Map        ------------------------>    字典 自己学习发现,java跟python这两门面向对象语言在数据类型的定义上,很多思想都是互通的,这里不说java,简单介绍一下python的列表,元组和字典. 一.列表 List: 最通

python中列表 元组 字典 集合的区别

列表 元组 字典 集合的区别是python面试中最常见的一个问题.这个问题虽然很基础,但确实能反映出面试者的基础水平. (1)列表 什么是列表呢?我觉得列表就是我们日常生活中经常见到的清单.比如,统计过去一周我们买过的东西,把这些东西列出来,就是清单.由于我们买一种东西可能不止一次,所以清单中是允许有重复项的.如果我们扩大清单的范围,统计我们过去一周所有的花费情况,那么这也是一个清单,但这个清单里会有类别不同的项,比如我们买东西是一种花费,交水电费也是一种花费,这些项的类型是可以使不同的.pyt

笨方法学python(6)加分题--列表与字典的区别

he string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIG

Python的列表&amp;元组&amp;字典&amp;集合

目录 列表(list) 列表的定义 列表的查询 增加数据 修改数据 删除数据 其它常用操作 元组(tuple) 元组的拆包 具名元组 字典(dict) 创建字典 字典添加数据 查询字典数据 修改字典数据 删除字典数据 其它操作 字典的遍历 集合(set) 集合的创建 访问集合 更新集合 删除集合 集合的操作符 集合应用 列表(list) ? 列表(list)是python以及其他语言中最常用到的数据结构之一.Python使用中括号 [ ] 来解析列表.列表是可变的(mutable)--可以改变列

python 列表生成、元组、字典

*列表生成 1语法: range(starti  stop step)         2.7版本 list(range(starti  stop step))   3.5 2.通过循环生成列表: [x for x in range(1,11)  if] *元组: 1什么是元组:也是一种容器类型,元组的元素不能修改,元组使用小括号包裹数据 2.如何创建:在括号中添加元素,用逗号隔开 1. tup1=('kate','lisa',1997,2000) tup2=(1,2,3,4,5) tup3='

python数据类型—列表、元组、字典

*列表生成 *元组 *字典 以下实验均由2.7版本操作完成 一.列表生成 1.语法: range(starti  stop  step)         2.7版本 list(range(starti  stop  step)))    3.5版本 生成一个从数字1到10的列表: print(list.pop()); a=list(range(1,10)); print(a)  ## [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 2.通过循环生成列表: 语法:[x for x