• Intro to Python for Data Science Learning 2


    List

    from:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-2-python-lists?ex=2

    -----------------1-------------------------------------

    • Create a list

    As opposed to intbool etc., a list is a compound data type; you can group values together:

    a = "is"
    b = "nice"
    my_list = ["my", "list", a, b]

    --------------2----------------------------------------

    • Create list with different types

    A list can contain any Python type. Although it's not really common, a list can also contain a mix of Python types including strings, floats, booleans, etc.

    # area variables (in square meters)
    hall = 11.25
    kit = 18.0
    liv = 20.0
    bed = 10.75
    bath = 9.50

    # Adapt list areas
    areas = ["hallway",hall, "kitchen",kit, "living room", liv, "bedroom",bed, "bathroom", bath]

    # Print areas
    print(areas)

    ---------------3-------------------------------------

    •  Select the valid list
    • my_list = [el1, el2, el3]

     both of them are correct:A. [1, 3, 4, 2] B. [[1, 2, 3], [4, 5, 7]] C. [1 + 2, "a" * 5, 3]

    Subsetting lists

    • Subset and conquer

    x = ["a", "b", "c", "d"]
    x[1]
    x[-3] # same result!
    • Subset and calculate

    x = ["a", "b", "c", "d"]
    print(x[1] + x[3])
    •  
    • Slicing and dicing

    x = ["a", "b", "c", "d"]
    x[1:3]

    The elements with index 1 and 2 are included, while the element with index 3 is not.

    • Slicing and dicing (2)

    x = ["a", "b", "c", "d"]
    x[:2]
    x[2:]
    x[:]
    •  
    • Subsetting lists of lists

    x = [["a", "b", "c"],
         ["d", "e", "f"],
         ["g", "h", "i"]]
    x[2][0]
    x[2][:2]

    List Manipulation

    --------------------1-----------------------

    Replace list elements

    x = ["a", "b", "c", "d"]
    x[1] = "r"
    x[2:] = ["s", "t"]


    -------------------2------------

    Extend a list

    x = ["a", "b", "c", "d"]
    y = x + ["e", "f"]

    -------------------3------------

    Delete list elements

    x = ["a", "b", "c", "d"]
    del(x[1])

    Pay attention here: as soon as you remove an element from a list, the indexes of the elements that come after the deleted element all change!
    The ; sign is used to place commands on the same line. The following two code chunks are equivalent:
    # Same line
    command1; command2
    
    # Separate lines
    command1
    command2

    So if delete two items, pay more attention here. better to use like this del(areas[-4:-2]), instead of "del(areas[10]); del(areas[11])","del(areas[10:11])" "del(areas[-3]); del(areas[-4])",

    ----------------------------------------------4--------------------------------------------------------------

    Inner workings of lists

     if use "areas_copy = areas", then they are the same thing, when value in one of them changed, both of them will be changed.

    if use "areas_copy = areas[:]", then they are not the same thing, when value in one of them changed, the other one will Not be changed.



  • 相关阅读:
    用于Delphi的DevExpress VCL组件——富文本编辑功能升级
    5个技巧,教你优化React App性能
    DevExpress WPF模板库助力快速完成界面美化
    Web开发小技巧放送
    WinForm应用界面开发入门指南
    浅析C#中单点登录的原理和使用
    Wireshark抓包,带你快速入门
    你必须知道的EF知识和经验
    你知道SqlDataAdapter中的Fill是怎么实现的吗
    一文说通C#中的异步编程
  • 原文地址:https://www.cnblogs.com/keepSmile/p/7694061.html
Copyright © 2020-2023  润新知