Python basic learning notes - lists, dictionaries, tuples, collections

Posted by inquisitive on Fri, 21 Jan 2022 16:11:14 +0100

1. List

A list is equivalent to an array, but it can store data of multiple and different data types.

Features of the list:

  1. Orderly arrangement
  2. The index maps a unique data
  3. There can be duplicate data
  4. Data types can be mixed
  5. Dynamically allocate and reclaim memory

(1) List creation:

Method 1: directly define with [], method 2: create with built-in function list()

list1 = ['hello', 'world', 520, 'hello', 13.14]
list2 = list(['hello', 'world', 520, 'hello', 13.14])

(2) Get

<1> Gets the index from the specified element

  1. When there are multiple identical elements, the index of the first element is returned
    print(list1.index('hello'))    #At this time, the output result is: 0
  2. If the element found does not exist, a valueError is thrown
  3. You can find between the specified start and stop positions
    print(list1.index('hello', 1, 4))    #The output result is: 3
    #Find hello from position 1 to position 4 (but not including position 4).

<2> Gets a single element in a list by index

  1. Forward index: from 0 to N-1
    print(list1[0], list1[4])    #At this time, the output result is: Hello 13.14, and hello is the first hello
  2. Reverse index: from - N to - 1
    print(list1[-5], list1[-1])    #At this time, the output result is: hello 13.14, and hello is the first hello
  3. When the specified index does not exist, an IndexError is thrown

<3> Gets multiple elements in the list

List name [start: stop: step]. Start and stop represent the slice range, and start is omitted. It starts from 0 by default; Stop is omitted and defaults to the last element. Step defaults to 1.

list = [10,20,30,40,50,60,70,80,90]
print(list[1:6:1])    #The output result at this time is: [20, 30, 40, 50, 60]
# list[1:6:1] is equivalent to list[1:6] is equivalent to list[1:6:]

When the value of step is negative:

The first element of the slice defaults to the last element of the list

The last element of the slice defaults to the first element of the list

print(list[ : :-1])     #The output result at this time is: [90, 80, 70, 60, 50, 40, 30, 20, 10]
print(list[7: :-1])     #The output result at this time is: [80, 70, 60, 50, 40, 30, 20, 10]
print(list[6:0:-2])     #The output result at this time is: [70, 50, 30]

(3) Addition of list elements

  1. append(): add an element at the end
    list.append(100)
    print(list)      #[10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
  2. extend(): add at least one element at the end: if append is used, add will be added_ List is added to the list as an element
    add_list = ['hello','world']
    list.append(add_list)
    print(list)      #[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, ['hello', 'world']]

    If you use extend(), add_ Every element in the list is added to the list

    add_list = ['hello','world']
    list.extend(add_list)
    print(list)      #[10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 'hello', 'world']
  3. insert(): add an element x at a certain position i
    list.insert(1,90)
    print(list)      #[10, 90, 20, 30, 40, 50, 60, 70, 80, 90, 100, 'hello', 'world']
  4. Slice, add N elements at any position to replace the cut elements
    new_list = [True, False, 'love']
    list[1:] = new_list
    print(list)      #[10, True, False, 'love']

(4) Deletion of list elements

  1. remove(): delete one element at a time, and delete the first element of the same element
    list = [10,20,30,40,50,60,70,80,30]
    list.remove(30)
    print(list)     #[10, 20, 40, 50, 60, 70, 80, 30]
  2. pop(): delete the element at the specified position. If no parameter is given, the element at the last position will be deleted by default
    list.pop(1)
    print(list)     #[10, 40, 50, 60, 70, 80, 30]
  3. Slice, but a new list object is generated
    new_list = list[1:3]
    print(new_list) #[40, 50]
    print(list)     #[10, 40, 50, 60, 70, 80, 30]
  4. clear(): clear all elements
    list.clear()
    print(list)     #[]
  5. del: delete list object
    del list

(5) modification of list elements

  1. Modify using the location of the index
    list = [10,20,30,40,50,60,70,80,30]
    list[2] = 100
    print(list)      #[10, 20, 100, 40, 50, 60, 70, 80, 30]
  2. Assign new values by slicing
    list[1:3] = [300,400,500,600]
    print(list)      #[10, 300, 400, 500, 600, 40, 50, 60, 70, 80, 30]

(6) Sorting of list elements

sort(): sort from small by default. If reverse = True is specified, sort from large to small:

list = [10,70,30,60,40,20,90,50,40]
list.sort()
print(list)   #[10, 20, 30, 40, 40, 50, 60, 70, 90]
list.sort(reverse=True)
print(list)   #[90, 70, 60, 50, 40, 40, 30, 20, 10]

(7) List generation

The expression of the list generation element ¢ for ¢ custom variable ¢ iteratable object

list = [i for i in range(1,10)]
print(list)      #[1, 2, 3, 4, 5, 6, 7, 8, 9]
list2 = [i * i for i in range(1,10)]
print(list2)     #[1, 4, 9, 16, 25, 36, 49, 64, 81]