zoukankan      html  css  js  c++  java
  • 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'}}
    >>> 
  • 相关阅读:
    如何导入文件夹在项目中
    KVC的学习
    ScrollView学习01
    KVO与KVC整理资料
    KVO监听者
    进程与线程
    解决SQL 2008数据库日志文件过大导致占满整个分区的问题:清理数据库日志文件
    SharePoint开发中发现的SharePoint本身的一些问题
    从十个方面提升SharePoint网站性能
    解决SharePoint2010文档库中新建文档不是保存到文档库而是保存到本地电脑的问题
  • 原文地址:https://www.cnblogs.com/ghostwu/p/8672278.html
Copyright © 2011-2022 走看看