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

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

    4.4.1 切片

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    print (players[0:3])

    ['charles', 'martina', 'michael']

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    print (players[1:4])

    ['martina', 'michael', 'florence']

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    print (players[:4])

    ['charles', 'martina', 'michael', 'florence']

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    print (players[2:])

    ['michael', 'florence', 'eli']

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    print (players[-3:])

    ['michael', 'florence', 'eli']

    4.4.2 遍历切片

    players = ['charles', 'martina', 'michael', 'florence', 'eli']
    
    print ("Here are the first three players on my team:")
    for player in players[:3]:
        print (player.title())

    Here are the first three players on my team:
    Charles
    Martina
    Michael

    4.4.3 复制列表

    my_foods = ['pizza', 'falafel', 'carrot cake']
    friend_foods = my_foods[:]
    
    print ("My favorite foods are:")
    print (my_foods)
    
    print ("
    My friend's favorite foods are:")
    print (friend_foods)

    My favorite foods are:
    ['pizza', 'falafel', 'carrot cake']

    My friend's favorite foods are:
    ['pizza', 'falafel', 'carrot cake']

    my_foods = ['pizza', 'falafel', 'carrot cake']
    friend_foods = my_foods[:]
    
    my_foods.append('cannoli')
    friend_foods.append('ice cream')
    
    print ("My favorite foods are:")
    print (my_foods)
    
    print ("
    My friend's favorite foods are:")
    print (friend_foods)

    My favorite foods are:
    ['pizza', 'falafel', 'carrot cake', 'cannoli']

    My friend's favorite foods are:
    ['pizza', 'falafel', 'carrot cake', 'ice cream']

    my_foods = ['pizza', 'falafel', 'carrot cake']
    friend_foods = my_foods
    
    my_foods.append('cannoli')
    friend_foods.append('ice cream')
    
    print ("My favorite foods are:")
    print (my_foods)
    
    print ("
    My friend's favorite foods are:")
    print (friend_foods)

    My favorite foods are:
    ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

    My friend's favorite foods are:
    ['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

    4.5.1 定义元组

    dimensions = (200, 50)
    print (dimensions[0])
    print (dimensions[1])

    200
    50

    dimensions = (200, 50)
    dimensions[0] = 250

    dimensions[0] = 250
    TypeError: 'tuple' object does not support item assignment

    4.5.2 遍历元组中的所有值

    dimensions = (200, 50)
    for dimension in dimensions:
        print (dimension)

    200
    50

    4.5.3 修改元组变量

    dimensions = (200, 50)
    print ("Origin dimensions:")
    for dimension in dimensions:
        print (dimension)
    
    dimensions = (400, 100)
    print ("
    Modified dimensions")
    for dimension in dimensions:
        print (dimension)

    Origin dimensions:
    200
    50

    Modified dimensions
    400
    100

    Caesar卢尚宇

    2020年3月12日

  • 相关阅读:
    程序员常见的坏习惯,你躺枪了吗?
    程序员常见的坏习惯,你躺枪了吗?
    程序员常见的坏习惯,你躺枪了吗?
    ACM2037
    [Golang]字符串拼接方式的性能分析
    如果一个类同时继承的两个类都定义了某一个函数会怎样呢 | Code4Fun
    Python学习笔记(四)函数式编程
    MySql之增删改查 · YbWork's Studio
    季銮西的博客
    ActiveMQ学习总结(一)
  • 原文地址:https://www.cnblogs.com/nxopen2018/p/12468862.html
Copyright © 2011-2022 走看看