Day 7 course summary - list and list derivation

Posted by wenquxing on Wed, 08 Dec 2021 04:15:58 +0100

Day 7 course summary - list and list derivation

1, List related operators

1. Mathematical operators: +, * can be used for operations between lists

1) List 1 + list 2 - combine the elements in the two lists to produce a new list, such as:

list1 = [100, 200, 300]
list2 = [10, 20]
print(list1 + list2) #The result is [100, 200, 300, 10, 20]

2) List * N / N * list - the elements in the list are repeated N times to produce a new list, such as:

print(list2 * 3)        # [10, 20, 10, 20, 10, 20]
print(list2 * 1)        # [10, 20]
  1. Comparison operators: >, <, > =, < =, = ==

    Add: different types can be = = and= To compare equality, but you cannot use >, <, > =, < = to compare size

    1)==,!=

    print([10, 20, 30] == [10, 20, 30])     # True
    print([10, 20, 30] == [10, 30, 20])     # False (list ordered)
    print({10, 20, 30} == {30, 20, 10})     # True (collection out of order)
    

2)>,<,>=,<=

The two lists compare the size of the first pair of unequal elements, such as:

print([10, 100, 200, 300] > [20, 1])
print([10, 100, 200, 300] > [10, 20, 100000000, 800, 9000])
print([10, 20, 30] > [10, 20])

2, List correlation function

  1. max,min
    Max (sequence) - gets the largest element in the sequence
    Min (sequence) - gets the smallest element in the sequence
nums = [10, 29, 78, 34, 56, 5, 72]
print(max(nums))
print(min(nums))

Note: when it comes to taking the maximum and minimum values, the elements in the sequence should be able to compare their sizes, otherwise an error will be reported, for example:

nums = [10, 'abc', 23, 8]
print(max(nums))      # report errors!

2.sum

Sum (number sequence) - sum all elements in the sequence, for example:

nums = [10, 29, 78, 34, 56, 5, 72]
print(sum(nums))
print(sum(range(101)))

3.sorted
Sorted - sorts the elements in the sequence from small to large to produce a new list
Sorted (sequence, reverse=True) - sort the elements in the sequence from large to small to generate a new list; Example:

nums = [10, 29, 78, 34, 56, 5, 72]
new_nums = sorted(nums)
print(new_nums)     # [5, 10, 29, 34, 56, 72, 78]

nums = [10, 29, 78, 34, 56, 5, 72]
new_nums = sorted(nums, reverse=True)
print(new_nums)     # [78, 72, 56, 34, 29, 10, 5]
  1. len
    Len (sequence) - gets the number of elements in the sequence
print(len([10, 20, 30])) #3
print(len('abc123')) #6
print(len(range(5, 100))) #95

5.list

List - creates a new list with the elements of the sequence as the elements of the list

print(list('abc'))          # ['a', 'b', 'c']
print(list(range(3)))       # [0, 1, 2]

3, List related methods

List. Append (element) - adds an element at the end of the list
List. Insert (subscript, element) - the added element precedes the subscript element
List. Remove (element) - deletes the corresponding element
List. pop() - take out the last element and return

List. Pop (subscript) - retrieves the element corresponding to the specified subscript and returns

  1. List. clear() - clear the list

    nums = [10, 20, 34, 89]
    nums.clear()
    print(nums)     # []
    
  2. Listing.copy() as like as two peas, the copy list produces a new list that is exactly the same.
    List. copy() - shallow copy
    List [:], list * 1 and list + [] are all shallow copies

Note: when saving data, variables actually save the address of the data in memory (all variables in Python are pointer variables). One variable directly assigns an address to another variable

nums = [10, 20, 34, 89]
new_nums1 = nums.copy()
print(new_nums1)         # [10, 20, 34, 89]

new_nums2 = nums
print(new_nums2)        # [10, 20, 34, 89]

print('nums of id:', id(nums))             # 4366826816
print('new_nums1 of id:', id(new_nums1))       # 4366516864
print('new_nums2 of id:', id(new_nums2))       # 4366826816
  1. List. Count (element) - counts the number of specified elements in the list

    nums = [10, 20, 34, 89, 10, 34, 80, 10]
    print(nums.count(10))    # 3
    
    c1 = nums.count(5)
    print(c1)       # 0
    
  2. List. Extend (sequence) - add all the elements in the sequence to the list; Example:

    nums = [100, 200]
    # nums.append([10, 20])
    # print(nums)   # [100, 200, [10, 20]]
    
    nums.extend([10, 20])
    print(nums)     # [100, 200, 10, 20]
    
    nums.extend('abc')
    print(nums)     # [100, 200, 10, 20, 'a', 'b', 'c']
    
  3. List. Index (element) - get the subscript value of the element in the list (subscript value starting from 0)
    If the element has more than one subscript, return the first one; If the element does not exist, an error will be reported. Example:

nums = [10, 20, 34, 89, 10, 34, 80, 10]
result = nums.index(20)
print(result)       # 1

result = nums.index(89)
print(result)       # 3

result = nums.index(10)
print(result)       # 0

# result = nums.index(100)          # Error reporting: ValueError: 100 is not in list

6. List. reverse() - list in reverse order

nums = [10, 28, 90, 67, 20]
nums.reverse()
print(nums)     # [20, 67, 90, 28, 10]


nums = [10, 28, 90, 67, 20]
result = nums[::-1]
print(result)       # [20, 67, 90, 28, 10]
  1. sort

    List. sort() / list. sort(reverse=True)
    Sorted / sorted (sequence, reverse=True)

    nums = [10, 28, 90, 67, 20]
    result = nums.sort()
    print(nums)     # [10, 20, 28, 67, 90]
    print(result)   # None does not create a new list, but sorts the elements in the original list in the original list
    
    nums = [10, 28, 90, 67, 20]
    result = sorted(nums)
    print(nums)     # [10, 28, 90, 67, 20]
    print(result)   # [10, 20, 28, 67, 90]
    

4, List derivation

1. Derivation structure 1 (generally used for list element transformation)

[expression for variable in sequence]-

Note: let the variables take values from the sequence one by one. Until the value is taken, the value of the expression will be taken as an element of the list for each value

list1 = [10 for x in 'abc']
print(list1)        # [10, 10, 10]

list2 = [x*2 for x in range(5, 11)]
print(list2)        # [10, 12, 14, 16, 18, 20]

2. Derivation structure 2 (generally used when the elements of the list need to be filtered)

[expression for variable in sequence if conditional statement]

Description: create a list;
Variables are taken one by one until they are taken. Each value is taken to judge whether the conditional statement is True. If it is True, the result of the calculation expression is the element of the list

result = [x for x in range(10, 21) if x % 2]
print(result)   # [11, 13, 15, 17, 19]

3. Supplementary three item operator

How to write: value 1 if expression else value 2 - if the value of the expression is True, the result is value 1, otherwise the result is value 2

example:

age = 8
result = 'adult' if age >= 18 else 'under age'
print(result)

5, Tuple

Tuples are immutable lists
Query, in and not in, mathematical operation, comparison operation and related function tuple are supported

1. What is a tuple

(element 1, element 2, element 3,...)
Immutable; Orderly

Empty tuple

t1 = ()

Tuple with one element: (element,)

t2 = (10,) Note: the tuple of an element should be followed by a comma

Topics: Algorithm leetcode