zoukankan      html  css  js  c++  java
  • 《Python编程从入门到实践》_第三章_列表简介

    什么是列表呢?

    官方说明就是由一些列按特点顺序排列的元素组成。其实可以看出很多个字符串的有序组合吧,里面的内容可以随时的删除,增加,修改。
    下面这个就是一个列表,python打印列表的时候会将中括号和引号打印出来的
    >>> name = ['liubin','liujian','liuliu']
    >>> print (name)
    ['liubin', 'liujian', 'liuliu']
    

    有序的列表

    和大多数编程语言一样,列表的第一个元素的索引是0,而不是1。如果要输出最后一个元素可以使用-1,倒数第二个可以使用-2,以此类推。

    >>> language = ['python','C++','Java','PHP','Ruby']
    >>> print (language[0])
    python
    >>> print (language[2])
    Java
    >>> print (language[-1])
    Ruby
    >>> print (language[-2].lower())
    php
    

    列表的修改、添加和删除元素

    修改:其实就是对元素的重新赋值

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki']
    motorcycles[0] = 'ducati'
    print(motorcycles)
    ['ducati', 'yamaha', 'suzuki']

    在末尾添加:append()

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki']
    motorcycles.append('ducati')
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki', 'ducati']

     在列表中插入:insert(索引,字符串)

    motorcycles = ['honda', 'yamaha', 'suzuki']
    motorcycles.insert(0, 'ducati')
    print(motorcycles)

    del语句删除元素,需要知道元素在列表中索引,删除后,无法再次访问它了

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki']
    del motorcycles[0]
    print(motorcycles)
    ['yamaha', 'suzuki']
    

    pop()弹出元素,默认是弹出最顶层的元素,就是最后一个,如果知道元素在列表中的位置,可以添加索引弹出

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki']
    popped_motorcycle = motorcycles.pop()
    print(motorcycles)
    ['honda', 'yamaha']
    print(popped_motorcycle)
    suzuki
    motorcycles = ['honda', 'yamaha', 'suzuki'
    first_owned = motorcycles.pop(0)
    print('The first motorcycle I owned was a ' + first_owned.title() + '.')
    The first motorcycle I owned was a Honda.

    有的时候你不知道元素的位置,但是知道元素的内容,可以使用remove('字符串')或者remove(变量)来删除,remove()只能删除第一个指定的值,删除的值可能在列表中出现多次,就需要使用后面学习到的循环了。

    motorcycles = ['honda', 'yamaha', 'suzuki', 'ducati']
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki', 'ducati']
    motorcycles.remove('ducati')
    print(motorcycles)
    ['honda', 'yamaha', 'suzuki']

     永久性排序

    方法sort()对列表进行永久的排序

    >>> car = ['bmw','audi','toyota','subaru']
    >>> print (car)
    ['bmw', 'audi', 'toyota', 'subaru']
    >>> car.sort()
    >>> print (car)
    ['audi', 'bmw', 'subaru', 'toyota']
    >>> car.sort(reverse=True)
    >>> print (car)
    ['toyota', 'subaru', 'bmw', 'audi']

     向方法传递参数reverse=Ture可以倒着排序

    临时性排序

     函数sorted()对列表进行临时的排序

    >>> car = ['bmw','audi','toyota','subaru']
    >>> print (sorted(car))
    ['audi', 'bmw', 'subaru', 'toyota']
    >>> print (car)
    ['bmw', 'audi', 'toyota', 'subaru']

     如果你要按与字母顺序相反的顺序显示列表,也可以向函数sorted()传递参数reverse=Ture

    翻转列表

     方法reverse()反正列表

    >>> car = ['bmw','audi','toyota','subaru']
    >>> car.reverse()
    >>> print (car)
    ['subaru', 'toyota', 'audi', 'bmw']
    

    求列表的长度

     函数len()可以知道列表的长度,python计算元素个数的时候,是从1开始的。

    >>> car = ['bmw','audi','toyota','subaru']
    >>> len(car)
    4
  • 相关阅读:
    【java8】慎用java8的foreach循环(作废)
    【Java并发系列03】ThreadLocal详解
    【Java并发系列04】线程锁synchronized和Lock和volatile和Condition
    【Java并发系列02】Object的wait()、notify()、notifyAll()方法使用
    【Java并发系列01】Thread及ThreadGroup杂谈
    java安全管理器SecurityManager入门
    【DWR系列06】- DWR日志及js压缩
    时间插件
    springMVC中Restful支持
    面向接口编程
  • 原文地址:https://www.cnblogs.com/liubinsh/p/6937502.html
Copyright © 2011-2022 走看看