Day 6 list derivation

Posted by ven0m on Wed, 08 Dec 2021 06:19:44 +0100

1. Mathematical operators: +*

List 1 + list 2 - merge elements from two lists to produce a new list

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

List * N / N * list - the elements in the list are repeated N times to produce a new list
print(list2 * 3) # [10, 20, 10, 20, 10, 20]
print(list2 * 1) # [10, 20]

2. Comparison operators: >, <, > =, < =, = ==

Add: different types can be = = and= To compare equality, but you cannot use >, <, > =, < = to compare size
The two lists compare the size of the first pair of unequal elements

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))

2. sum

Sum (digital sequence) - sum all elements in the sequence

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 produce a new list

nums = [89, 45, 30, 78, 90, 34, 99, 23, 9, 25]
new_nums = sorted(nums)
print(new_nums)
new_nums = sorted(nums, reverse=True)
print(new_nums)  # Null value, should be print(nums)

4.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]

List. Append (element)

List. Insert (subscript, element)

List. Remove (element)

List (. Pop)

1. List. clear() - clear the list

nums = [89, 45, 30, 78, 90]
nums.clear()
print(nums)

2. list as like as two peas,.copy() - copy list to produce a new list that is exactly the same.

List. copy() - shallow copy
List [:], list * 1 and list + [] are all shallow copies

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

When saving data, variables save the address of the data in memory (all variables in python are pointer variables).
One variable assigns an address to another variable.

3. List. Count (element) - counts the number of specified elements in the list

nums = [89, 45, 30, 90, 30, 90]
c1 = nums.count(90)
print(c1)      # 2

4. List. Extend (sequence) - adds all the elements in the sequence to the list

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']

5. 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.

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

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

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

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

7. Sorting

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

nums = [10, 28, 90, 67, 20]
result = sorted(nums)
print(nums)     # [10, 28, 90, 67, 20]
print(result)   # [10, 20, 28, 67, 90]

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

ternary operator

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

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

List derivation - an expression that quickly creates a list

1. Derivation structure 1

[expression for variable in sequence] - let the variables take values in the sequence one by one until they are taken. For each value taken, the value of the expression will be taken as an element of the list

# Exercise 1: use list derivation to get the single digits of all elements in nums
# [3, 9, 0, 6, 1, 2]
nums = [23, 89, 90, 56, 41, 802]
print([x % 10 for x in nums])

# Exercise 2: multiply all elements in nums by 10 using list derivation
# [230, 890, 900, 560, 410, 8020]
nums = [23, 89, 90, 56, 41, 802]
print([x * 10 for x in nums])

# Exercise 3: given the score list, replace all the elements in the list with 'pass' - True or' fail '- False
# ['pass',' pass', 'fail', 'pass',' fail ',' fail ']
#  [True, True, False, True, False, True]
scores = [90, 78, 45, 67, 39, 89]
scores1 = [x >= 60 for x in scores]
print(scores1)
scores2 = ['pass' if x >= 60 else 'fail,' for x in scores]
print(scores2)

# Exercise 4: given a list of numbers, divide all the even numbers in the list by 2 and multiply all the odd numbers by 2
# [46, 178, 45, 28, 82, 401]
nums = [23, 89, 90, 56, 41, 802]
print([x // 2 if x % 2 == 0 else x * 2 for x in nums])

2. Derivation structure 2

[expression for variable in sequence if conditional statement]

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

# Exercise 1: extract all odd numbers in nums using list derivation
# [23, 89, 41]
nums = [23, 89, 90, 56, 41, 802]
nums1 = [x for x in nums if x % 2]
print(nums1)

# Exercise 2: given a list, extract all strings in the list
list1 = [10, 23.9, 'hello', True, '12', 0]
print([x for x in list1 if type(x) == str])

# Exercise 3: given a list, extract all integers in the list and add 3 to the integers
# [13, 3]
list1 = [10, 23.9, 'hello', True, '12', 0]
print([x + 3 for x in list1 if type(x) == int])

task

  1. Create a list with 10 numbers in the list to ensure the order of elements in the list, arrange the weight of the list, and sort the list in descending order
For example:[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
		--- After weight removal [70, 88, 91, 107, 234, 177, 282, 197]
  	---- Descending sort [282, 234, 197, 177, 107, 91, 88, 70]
nums = [70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
new_nums = sorted(list(set(nums)), reverse=True)
print(new_nums)
  1. Using the list derivation, complete the following requirements

a. Generate a data list with 3 digits in 1-100

The result is [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
nums = [x for x in range(1, 101) if x % 10 == 3]
print(nums)

b. The use of list push is to extract the integers in the list

For example:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
nums = [True, 17, "hello", "bye", 98, 34, 21]
print([x for x in nums if type(x) == int])

c. Use the list derivation to store the length of the string in the specified list

For example: ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
nums = ["good", "nice", "see you", "bye"]
print([len(x) for x in nums])

d. Use list derivation to delete elements with integer single digits less than 5 in the list

For example:[24, 'abc', 99, True, 21, 38, 'hello'] --- ['abc', 99, True, 38, 'hello']
nums = [24, 'abc', 99, True, 21, 38, 'hello']
[nums.remove(x) for x in nums if type(x) == int and x % 10 < 5]
print(nums)

e. The element obtained by list derivation is the last element of each tuple in the list of tuples

For example:[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]  --- [30, 'hello', 3.4, False]
nums = [(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]
print([i[-1] for i in nums])

f. Use the list derivation to multiply all odd numbers in the number list by 2 and divide all even numbers by 2

for example: [23, 4, 67, 88, 90, 21]  -> [46, 2, 134, 44, 45, 42]
nums = [23, 4, 67, 88, 90, 21]
result = [x // 2 if x % 2 == 0 else x * 2 for x in nums]
print(result)
  1. Known as a list, get all the subscripts of the specified element in the list

    For example:[10, 20, 34, 10, 9, 78]
    10 Subscript of:[0, 3]
    20 Subscript of:[1]
    30 Subscript of:[]
    
nums = [10, 20, 34, 10, 9, 78]
for x in [10, 20, 30]:
    result = [i for i in range(len(nums)) if x == nums[i]]
    print(x, 'Subscript of:', result)
  1. *Given a list of numbers, the writer determines that the list is a continuously increasing list.

    For example:
    [1, 2, 3, 4, 5]   -> True
    [23, 45, 78, 90]  -> True
    [1, 3, 2, 4, 5]	-> False
    
nums = [1, 3, 2, 4, 5]
print(nums == sorted(nums))
  1. Given two lists, cross merge the two lists according to the following rules

    A = [10, 20, 30, 40, 50]
    B = [100, 200, 300]
    result:[10, 100, 20, 200, 30, 300, 40, 50]
    
a = [10, 20, 30, 40, 50]
b = [100, 200, 300]
new_a = a.copy()
c = []
for i in range(len(b)):
    c.append(a[i])
    del new_a[0]
    c.append(b[i])
print(c+new_a)
  1. Two sequential tables are known. Merge the two lists, and the elements in the merged new list are still incremental lists

    A = [10, 20, 30, 40, 50]
    B = [25, 44, 60]
    result:[10, 20, 25, 30, 40, 45, 50, 60]
    
A = [10, 20, 30, 40, 50]
B = [25, 44, 60]
print(sorted(A + B))

Topics: Python Algorithm