Record my learning process -Day03 python list / tuple / rang

First, a list of acquaintance

Python is one of a list of basic data types, which is based on '' [] 'in the form of enclosed, with each element "," separated, container data type belongs to, he can store a large number of various types The data.

  • The basic format

    li = ['Dylan', 'yaoyao', 1234, True, (1212, '中文'), ['列表', 'qiantao', {'name':'Dylan'}]]
    • Can be seen, a variety of data types may be stored within the list, or nested lists, tuples, dictionaries, and other Boolean value.

    • Compared to a list of strings, not only can store different types of data, and can store large amounts of data, 32-bit limit is python elements 536,870,912, restriction python 64 is 1152921504606846975 elements.

    • And the list is ordered, an index value, can be sliced, convenient value.

  • Create a list of

    • A common way :()

      l1 = [1, 33, 'Dylan']
    • Second way :( not used)

      l1 = list() # 默认是空列表
      #l1 = list(iterable) # 括号里是一个可迭代对象
      l1 = list('12345')
      print(l1) # ['1', '2', '3', '4','5']
    • Three ways: deriving a listing of formula (will be mentioned later)

      l1 = [i for i in range(1, 5)] 
      print(l1) # [1, 2, 3, 4]  注意看 列表内的数据类型是 int 型

Second, the list of index and sliced

  • index

    l1 = ['a', 'b', 'c', 'd', 'e', 'f']
    索引    0    1    2    3    4    5(-1)
    # 打印列表中索引0 的值并改回该值的数据类型
    print(l1[0], type(l1[0])) # a <class 'str'>
    # 打印列表中最后一个索引的值,在不知道列表长度时,一般用 -1 找到列表中最后一个索引的值。
    print(l1[-1])  # 等价于 print(l1[5])  
  • slice

    The slice is indexed by the interception of a list (index: Step size: Index), a new list is formed (that is, the principle of care regardless of tail)

    # 正取  切片规则:顾头不顾尾  格式:l1 = l[首:尾:步长]
    l = ['a', 'b', 'c', 'd', 'e', 'f']
    l1 = [1:3] # 取了'b','c' 并赋值给新列表 l1
    print(l1) # ['b', 'c']
    
    # 倒取  切片规则:顾头不顾尾  格式:l1 = l[首:尾:-步长]
    l = ['a', 'b', 'c', 'd', 'e', 'f']
    l1 = [4:1:-1] # 取了'e', 'd' ,'c' 并赋值给新列表 l1 注意:反取 步长为负数
    print(l1) # ['e', 'd', 'c']

Third, the increasing list / delete / change / check

Additions and deletions to change search list is a series of operations on a list of commonly used, is very important.

  • Increasing list

    • l.append (data to be inserted) to insert a new element to the list index last

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      l.append('Dylan')
      print(l)    # ['a', 'b', 'c', 'd', 'e', 'f', 'Dylan']

      For example : personnel entry procedures, the personnel list entry list, press Q to quit entry

      hr = []     # 建立一个空列表
      while 1:
          username = input("请输入你要录入的人员(退出请按'Q'):")
          if username.strip().upper() == 'Q':
              break
          else:
              hr.append(username)
      print(hr)
    • l.insert (index data to be inserted) is inserted into the index

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      l.insert(2,'Dylan')     #插入位置=索引位置,原索引位置数据后移一位
      print(l)    # ['a', 'b', 'Dylan', 'c', 'd', 'e', 'f']  
    • l.extend (iterables) Iterative insertion

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      l.extend('这是一个可迭代对象')
      # l.extend([1,2,3])   #会将列表拆分成单个元素,分别插入
      print(l)    #['a', 'b', 'c', 'd', 'e', 'f', '这', '是', '一', '个', '可', '迭', '代', '对', '象']
  • Delete list

    • l.pop (index) by index Removes and returns the deleted element

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      l.pop(2)    # 括号里写索引,可以将对应索引的元素删除,并可以反回给一个变量
      print(l)    # ['a', 'b', 'd', 'e', 'f']
      print(l.pop(2))     # d
      l.pop()     # 括号里不写索引,默认是删除最后一个,并可以将删除的元素反回给一个变量
      print(l)    # ['a', 'b', 'd', 'e', 'f']
      print(l.pop())      # e 
    • l.remove (element) by deleting elements if there are elements of the same name, delete the default first from the left

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      l.remove('f')
      print(l)    # ['a', 'b', 'c', 'd', 'e',]
    • l.clear () Clear List

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      l.clear()   # 清空列表,但列表还在,只不过是个空列表
      print(l)    # []
    • del Delete List

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      # 删除整个列表
      del l   # 将整个列表删除了
      # 按照索引删除
      del l[-1]
      print(l)    # ['a', 'b', 'c', 'd', 'e']
      # 按照切片(步长)删除
      del l[::2]
      print(l)     # ['a', 'c', 'e']
  • Change list

    • Direct change

      s = [1,2,3,'fjdkaj','fdsaeddd',1234,'ffdsaecc']
      s[0] = 'one'    #直接改,找到对应的索引,直接替换
      s[1] = ['one',1,2,3]    #还可以改成列表 list ,直接替换
      print(s)
    • Sliced ​​change

      s = [1,2,3,'fjdkaj','fdsaeddd',1234,'ffdsaecc']
      s[0:3] = 'one'    #把0到3拿出来,填上你改的,但是,是迭代的去改,以最小元素为单位
      s[0:3] = [1,2,3,8,8,] ##把0到3拿出来,迭代的填上你列表的最小元素,但是,是迭代的去改,以最小元素为单位
      print(s)    #打印内容:['o', 'n', 'e', 'fjdkaj', 'fdsaeddd', 1234, 'ffdsaecc']
  • Check list

    • Search by index

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      print(l[3])     # d
    • By slice (step) check

      l = ['a', 'b', 'c', 'd', 'e', 'f']
      print(l[1:4:2])     # b d
    • for i in the iterative search

      s = [1,2,3,'fjdkaj','fdsaeddd',1234,'ffdsaecc']
      for i in s:     #迭代查
          print(i)

Fourth, the nested list

  • Nested form

    l = [1, 2, 3, '1A', ['Dylan', 33],66]
    # 列表中的列表称之为嵌套列表,增删改查的方式与列表的相同,同样可以切片可以查改删增。

Quintuple

  • Tuple list is also known as read-only, can store large amounts of data, the index can be, slice (step).

  • The basic format of a list of lists, except that: a tuple is wrapped in parentheses, brackets and the list is a package together.

  • Format is as follows:

    t = (1, 2, 'Dylan', [1, 2, 3, 'a', 'b', 'c'])

    Note that: the number of tuples in the additions and deletions not well documented, but there is an exception, if nested within the tuple list, this list can be additions and deletions to the method with a list of CRUD.

  • Unpacking tuples

    Tuple unpacking typically used, such as:

    a, b = (1, 2)   # 多一个,少一个都不行。
    print(a, b) # 1 2

Six, range

  • Similar to the list, customized digital range of numbers list

    r = range(10) # [0,1,2,3,4,5,6,7,8,9]
    print(r)    # range(0, 10) 
    for i in r:
        print(i)    # 依次打印0到9的数字
    l = [1, 2, 3, '1A', ['Dylan', 33],66]
    for i in range(len(l)): # 将列表的索引依次赋值给变量 i
        print(i) # 打印 i (也就是列表的索引)
    # len() 获取数据的元素个数

Guess you like

Origin www.cnblogs.com/guanshou/p/12032106.html