zoukankan      html  css  js  c++  java
  • Python编程 从入门到实践-3列表下

    笔记出处(学习UP主视频记录) https://www.bilibili.com/video/av35698354?p=5

    3.2.3 从列表中删除元素-使用del语句删除元素

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

    ['honda', 'yamaha', 'suzuki']
    ['yamaha', 'suzuki']

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

    ['honda', 'yamaha', 'suzuki']
    ['honda', 'suzuki']

    3.2.3 从列表中删除元素-使用方法pop()删除元素

    motorcycles = ['honda', 'yamaha', 'suzuki']
    print (motorcycles)
    
    poped_motorcycle = motorcycles.pop()
    print (motorcycles)
    
    print (poped_motorcycle)

    ['honda', 'yamaha', 'suzuki']
    ['honda', 'yamaha']
    suzuki

    3.2.3 从列表中删除元素-弹出列表中任何位置处的元素

    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.

    3.2.3 从列表中删除元素-根据值删除元素

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

    ['honda', 'yamaha', 'suzuki', 'ducati']
    ['honda', 'yamaha', 'suzuki']

    3.3 组织列表

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

    cars = ['bmw', 'audi', 'toyota', 'subary']
    
    cars.sort()
    print (cars)

    ['audi', 'bmw', 'subary', 'toyota']

    cars = ['bmw', 'audi', 'toyota', 'subary']
    
    cars.sort(reverse=True)
    print (cars)

    ['toyota', 'subary', 'bmw', 'audi']

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

    cars = ['bmw', 'audi', 'toyota', 'subary']
    
    print ("Here is the origin list: ")
    print (cars)
    
    print ("
    Here is the sorted list: ")
    print (sorted(cars))
    
    print ("
    Here is the original list again: ")
    print (cars)

    Here is the origin list:
    ['bmw', 'audi', 'toyota', 'subary']

    Here is the sorted list:
    ['audi', 'bmw', 'subary', 'toyota']

    Here is the original list again:
    ['bmw', 'audi', 'toyota', 'subary']

    3.3.3 方法reverse()倒着打印列表

    cars = ['bmw', 'audi', 'toyota', 'subary']
    print (cars)
    
    cars.reverse()
    print (cars)

    ['bmw', 'audi', 'toyota', 'subary']
    ['subary', 'toyota', 'audi', 'bmw']

    3.3.4 函数len()确定列表的长度

    cars = ['bmw', 'audi', 'toyota', 'subary']
    
    print (len(cars))

    4

    3.4 使用列表时避免索引错误

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

    print (motorcycles[3])
    IndexError: list index out of range

  • 相关阅读:
    Oct 21st-
    ContextLoaderListener 解析
    HTTPS 证书制作及使用
    Spring MVC 源码分析
    思考
    《深入理解java虚拟机》 第七章虚拟机类加载机制
    《深入理解java虚拟机》第六章 类文件结构
    《深入理解java虚拟机》第三章 垃圾收集器与内存分配策略
    《深入理解java虚拟机》第二章 Java内存区域与内存溢出异常
    SSM-1第一章 认识SSM框架和Redis
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12468246.html
Copyright © 2011-2022 走看看