Python is vulnerable to a beginner practice (lists, tuples)

Python (lists, tuples)

A. List

  1. Acquaintance List

    • The list is one of the basic data types of python, other programming languages have similar data types
      such as arrays of JS, java, etc. in the array, which is surrounded by [], with each element "," spaced and you can store a variety of data types.
    • Compared to a list of strings, not only can store different types of data, and can store large amounts of data.
      When the Python 32 can be stored: 536,870,912 elements, can be stored as 64-bit Python: 1152921504606846975 elements, and the list is stored as you the sorting order, and may have an index slice value easily.
  2. List index, and slicing operations

    1.1 index list

    There are also a list of strings as index:

    # 列表索引的应用
    lst = ["asd", "你好", "hell world", 123, "升仙"]
    print(lst[0]) # 获取第一个元素
    print(lst[1]) # 获取第二个元素
    print(lst[2]) # 获取第三个元素
    print(lst[3]) # 获取第四个元素
    print(lst[4]) # 获取第五个元素
    # 列表是可变的与字符串不同这点要注意,在列表的操作中会有说明.

    List of sections 1.2

    String list slicing operation may be carried out as

    lst = ["asd","你好","hell world",123,"升仙"]
    print(lst[0:3]) # ["asd","你好","hell world"]
    print(lst[:3]) # ["asd","你好","hell world"]
    print(lst[1::2]) # ["你好",123] 列表同样有步长
    print(lst[2::-1]) # ["hell world","你好","asd"] 步长位置的正负控制着取值的方向
    print(lst[-1:-4:-2]) # ["升仙","hell world"]
    # 列表和字符串的操作基本类似,因此操作起来并不难举一反三即可.

    1.3 List operations (add, delete, change, other operations)

    Note: list and str are not the same, lst changes can occur, it can be manipulated directly on the original list objects.

    1.3.1 increase list

    # 在列表后新增
    lst = ["asd","你好","hell world",123,"升仙"]
    print(lst)
    lst.append("wusir")
    print(lst) # ["asd","你好","hell world",123,"升仙","wusir"]
    ---------------------------------
    # 根据索引位置插入
    lst = ["asd","你好","hell world",123,"升仙"]
    lst.insert(1,"成仙") # 在索引为1的位置插入刘德华,原来的元素向后移动一位.
    print(lst) # ["asd","成仙","你好","hell world",123,"升仙"]
    ---------------------------------
    # 迭代添加
    lst = ["asd","你好","hell world",123,"升仙"]
    lst2 = ["升仙成功","升仙失败"]
    lst.extend(lst2)
    print(lst) # ["asd","你好","hell world",123,"升仙","升仙成功","升仙失败"]
    print(lst2) # ["升仙成功","升仙失败"]
    # lst2列表添加到lst列表中,lst2本身不变.

    1.3.2 delete list

    There are four ways to delete the list (pop, remove, clear, del)

    # pop删除用法
    lst = ["asd","你好","hell world",123,"升仙"]
    print(lst)
    el = lst.pop() # 当pop()括号内为空时,默认删除最后一个索引所对应的的数据.
    print(el) # 打印结果:升仙,pop删除列表的某一项时,会有返回值,其返回值就是被pop删除的内容.
    print(lst) # ["asd","你好","hell world",123]
    
    lst = ["asd","你好","hell world",123,"升仙"]
    el2 = lst.pop(2) # 删除索引为2的位置的值
    print(el2) # hell world
    print(lst) # ["asd","你好",123,"升仙"]
    ---------------------------------
    # remove删除用法
    lst = ["asd","你好","hell world",123,"升仙"]
    lst.remove("升仙") # 可以删除指定元素,当删除不存在的元素会报错
    print(lst) # ["asd","你好","hell world",123]
    ---------------------------------
    # clear删除用法
    lst.clear() # 清空list
    print(lst) # []
    ---------------------------------
    # 切片删除
    del lst[1:3] # 强制删除
    print(lst) # ["asd",123,"升仙"]

    1.3.3 modify the list

    # 列表的修改
    lst = ["asd","你好","hell world",123,"升仙"]
    lst[1] = "飞升" # 把1号元素修改成飞升
    print(lst) # ["asd","飞升","hell world",123,"升仙"]
    lst[:4:2] = ["返璞归真", "融会贯通"] # 切片修改也可以,同时注意当步长不是1时,元素的个数要一一对应额否则会报错
    print(lst) # ["返璞归真","飞升","融会贯通",123,"升仙"]
    lst[1:4] = ["霸气外露"] # 如果切片没有步长或者步长是1时,则不用管替换的个数.
    print(lst) # ["返璞归真","霸气外露","升仙"]

    Query 1.3.4 list

    Is a list of objects can iterate

    # 用for查询打印列表每一个元素
    lst = ["asd","你好","hell world",123,"升仙"]
    for el in lst:
     print(el)
    ---------------------------------
    # 查询元素在列表中出现的次数
    lst = ["asd","你好","hell world",123,"升仙"]
    c = lst.count("升仙") # 查询升仙出现的次数
    print(c) # 1
    ---------------------------------
    # 查询列表的长度
    lst = ["asd","你好","hell world",123,"升仙"]
    l=len(lst) # 列表的长度,与字符串的使用方式相同
    print(l) # 5

    1.3.4 list of other actions

    # 升序
    lst = [1,2,15,23,4,33]
    lst.sort() # 排序,默认为升序.
    print(lst) # [1,2,4,15,23,33]
    ---------------------------------
    # 降序
    lst.sort(reverse=True) # 降序
    print(lst) # [33,23,15,4,2,1]
    ---------------------------------
    # 翻转(倒序)
    lst = ["asd","你好","hell world",123,"升仙"]
    lst.reverse() # 此方法只针对列表内的各个元素,倒序
    print(lst) # ["升仙",123,"hell world","你好","asd"]
  3. Nested list

    Layer by layer to see flexibility in the use, in context.

    lst = ["武当","峨眉","少林",["崆峒",["极乐谷"],"华山"],"魔教"]
    # 找到峨眉
    print(lst[2])
    # 找到少林和魔教
    print(lst[2:])
    # 找到崆峒
    print(lst[3][0])
    # 将崆峒改成丐帮
    lst[3][0]="丐帮"
    print(lst) # ['武当','峨眉','少林',['丐帮',['极乐谷'],'华山'],'魔教']
    # 在lst列表后添加"丐帮"
    lst.append("丐帮")
    print(lst) # ['武当','峨眉','少林',['崆峒',['极乐谷'],'华山'],'魔教','丐帮']

II. Tuple

  1. Tuple acquaintance

    Commonly known as immutable tuple list, also known as read-only list, one of the basic data types of tuples are python, with small brackets, which can put any data type, may also be cyclic, the query may, sliced also can be, is not changed.

    Note: This tuple immutable means when the sub-element is immutable immutable elements, when the sub-element is mutable object can be changed, depending on whether the child element is a variable object.

    Tuples if only one element, you need to add a comma, otherwise it is not a tuple, when tu (time =), print (type (tu)), print the results show tu tuple.

  2. Nested tuples

    Tuple there, len (), count (), index () methods, you can try it yourself until mastery.

    # 元组取值和切片
    tup = ("asd","你好","hell world",123,"升仙")
    print(tup[0]) # asd
    print(tup[2]) # hell world
    print(tup[2:]) # ("hell world",123,"升仙") 切片后仍然是元组
    ---------------------------------
    # for循环
    tup = ("asd","你好","hell world",123,"升仙")
    for el in tup:
     print(el)
    ---------------------------------
    # 当元组内的子元素时可变类型时可以进行操作,将武当换成丐帮
    tup = ("asd","你好","hell world",123,["武当","峨眉","少林"])
    tup[-1][0]="丐帮"
    print(tup) # ("asd","你好","hell world",123,["丐帮","峨眉","少林"])
    # 这里元组的不可变的意思是子元素不可变,而字元素内部的子元素是可以变, 这取决于子元素是否是可变对象.

Guess you like

Origin www.cnblogs.com/chenshuo531702820/p/10986271.html