• python基础(二)


    #列表简介
    #列表由一系列按特定顺序排列的元素组成,可以创建包含字母表中所有字母、数字或所有家庭成员姓名的列表

    a = ['q','t','m']
    print(a)#打印时包含方括号
    ['q', 't', 'm']
    

    #访问列表元素
    print(a[0])
    q

    print(a[0:2])#前闭后开
    ['q', 't']
    

    print(a[0].title())#大写
    Q
    

    print(a[1])
    print(a[2])
    t
    m
    
    print(a[-1])#最后一个元素
    m
    
    family = ['papa','mama','jingjing']
    message = "I love my family:" + family[0].title() + "" + family[1].title() + "" + family[2].title() + "!"
    print(message)
    I love my family:Papa、Mama、Jingjing!
    
    #修改、添加和删除元素
    a[0] = 'p'
    print(a)
    ['p', 't', 'm']

    a.append('sd')#在末尾添加元素
    print(a)
    ['p', 't', 'm', 'sd']
    
    #append这种创建列表的方式极其常见,可首先创建一个空列表,用于存储用户将要输入的值,然后将用户提供的每个新值附加到列表中
    a.insert(0,'ad')#在列表的第一个位置加入元素
    print(a)
    ['ad', 'ad']
    

    #从列表中删除元素
    b = ['hello','hi','baby']
    print(b)
    ['hello', 'hi', 'baby']

    popped_b = b.pop()#pop删除列表末尾的元素,并且让你可以接着使用它
    print(b)
    print(popped_b)
    ['hello', 'hi']
    baby
    

    last_do = b.pop()
    print(last_do.title())
    Hi
    

    b = ['hello','hi','baby']#用pop删除任何位置的元素,删掉之后可以再次使用,但是怎么使用??b.pop()
    first_do = b.pop(0)
    print(b)
    ['hi', 'baby']

    b = ['hello','hi','baby']#知道要删除的元素的值,但是并不知道位置
    b.remove('baby')
    print(b)
    ['hello', 'hi']
    

    #组织列表
    b = ['hello','hi','baby']
    b.sort()#按字母排序
    print(b)
    ['baby', 'hello', 'hi']
    

    b.sort(reverse = True)#按字母反向排序
    print(b)
    ['hi', 'hello', 'baby']
    
    #保持列表元素原来的列表顺序,同时以特定的顺序呈现它们
    b = ['hello','hi','baby']
    print("here is the original list:")
    print(b)
    
    print("
    here is the sorted list:")
    print(sorted(b))
    
    print("
    here is the original list again:")
    print(b)
    here is the original list:
    ['hello', 'hi', 'baby']
    
    here is the sorted list:
    ['baby', 'hello', 'hi']
    
    here is the original list again:
    ['hello', 'hi', 'baby']


    print("
    here is the sorted list:")
    print(sorted(b, reverse = True))###
    here is the sorted list:
    ['hi', 'hello', 'baby']

    f = ['d','M','Z']
    f.sort()
    print(f)#大小写都有的时候
    ['M', 'Z', 'd']


    b = ['hello','hi','baby']
    print(b)
    b.reverse()
    print(b)#倒着打印列表,永久性地修改表元素的排列顺序,但可以随时恢复到原来的排列顺序,只需再次reverse
    ['hello', 'hi', 'baby']
    ['baby', 'hi', 'hello']
    
    b = ['hello','hi','baby']
    print(len(b))
    3
    
















  • 相关阅读:
    python之简单窗口
    hdu1237简单计算器
    BST二叉查找树的实现与操作
    huffman树入门
    hdu2043密码
    hdu1870愚人节的礼物
    hdu 2085 核反应堆
    hdu 2066 一个人的旅行(Dijkstra求最短路问题)
    hdu 2063 过山车 (二分匹配)
    hdu 2067 小兔的棋盘(Catalan数)
  • 原文地址:https://www.cnblogs.com/Cookie-Jing/p/13585754.html
Copyright © 2020-2023  润新知