zoukankan      html  css  js  c++  java
  • 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
    
















  • 相关阅读:
    浮于文字上方的图片如何设置居中
    grub4dos_BIOS和grub4dos_UEFI编译环境搭建(ubuntu14.04)
    微信小程序开发——开发小技巧集锦
    vue+Better-scroll实现滚动位置保持并对页面切换效果进行优化
    谈谈fork/join实现原理
    ES系列(七):多节点任务的高效分发与收集实现
    JVM系列(五):gc实现概要01
    谈谈stream的运行原理
    ES系列(六):search处理过程实现1框架
    ES系列(五):获取单条数据get处理过程实现
  • 原文地址:https://www.cnblogs.com/Cookie-Jing/p/13585754.html
Copyright © 2011-2022 走看看