day6 - loop exercises and lists

List related operations

1. Mathematical operation of list: +*

1) List 1 + list 2 - merge two lists to produce a new list (List 1 and list 2 will not be modified)

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

2) List * N / N * list - the list repeats n times to produce a new list

result = list1 * 3    # list1 + list1 + list1
print(result)       # [10, 20, 30, 10, 20, 30, 10, 20, 30]

2. List comparison operation: = =,! =, >, <, > =<=

1) Compare equal

print([1, 2, 3] == [2, 3, 1])       # False
print([10, 20, 30] == [10, 20, 30])     # True

2) Compare size: >, <, > =<=
The two lists compare the size. The size of the first pair of unequal elements is compared (the elements in the same position are a pair of children)

print([10, 20, 30, 40, 50] > [10, 30])      # False
print([100, 200, 1, 1000, 10000] > [100, 200, 2, -100])    # False
print([100, 200, 3] > [100, 200])       # True

3. in and not in

Element in list - determines whether the specified element exists in the list
Element not in list - determines whether the specified element does not exist in the list

students = ['Zhang San', 'Li Si', 'floret']
print('Xiao Ming' in students)    # False
print('Zhang San' in students)     # True

Exercise 1: there is a list with a length of 10. There are 10 names in the array. It is required to remove the duplicate names

names = ['Zhang San', 'Li Si', 'chinese rhubarb', 'chinese rhubarb', 'Zhang San', 'Zhang San', 'Zhang San']
new_names = []
for name in names:
    if name not in new_names:
        new_names.append(name)
print(new_names)        # ['Zhang San', 'Li Si', 'rhubarb']

Exercise 2: there are two lists A and B. use list C to get the common elements in the two lists

A = [1, 'a', 4, 90, 'a']
B = ['a', 8, 'j', 1]
C = []
for x in A:
    if x in B and x not in C:
        C.append(x)
print(C)

Logic of in operation

item = 10
nums = [20, 34, 9, 10]
for x in nums:
     if x == item:
         print(True)
         break

List related functions and related methods

1. List related functions

1) max,min
max (sequence) - gets the maximum value of the element in the sequence
min (sequence) - gets the minimum value of the element in the sequence
Note: the element types in the sequence must be consistent; The element itself supports the operation of comparing sizes

nums = [23, 45, 78, 90, 12.9]
print(max(nums))
print(min(nums))

2) Sum (number sequence) - find the sum of all elements in the number sequence

  nums = [23, 45, 78, 90, 12.9]
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 (the order of the elements in the original sequence is not modified)
Sorted (sequence, reverse=True) - sort the elements in the sequence from large to small to generate a new list (the order of the elements in the original sequence is not modified)

nums = [23, 45, 78, 90, 12.9]
result = sorted(nums)
print(result)       # [12.9, 23, 45, 78, 90]

result = sorted(nums, reverse=True)
print(result)       # [90, 78, 45, 23, 12.9]

4) Len (sequence) - get the length of the sequence (the number of elements in the sequence)

nums = [23, 45, 78, 90, 12.9]
print(len(nums))

5) List - converts the specified data into a list
All sequences can be converted into lists, and the conversion is to convert the elements in the sequence into the elements of the list

result = list('abc')
print(result)       # ['a', 'b', 'c']

result = list(range(10, 15))
print(result)       # [10, 11, 12, 13, 14]

2. List related methods: list. Method name ()

1) List. clear() - clear the list

nums = [23, 45, 78, 90, 12.9]
nums.clear()
print(nums)     # []

2) list as like as two peas (.Copy) - copy the original list to produce a new list that is exactly the same.
List [:] = = list * 1 = = list + [] = = list. copy()

nums = [23, 45, 78, 90, 12.9]
result1 = nums.copy()
result2 = nums

print(nums)         # [23, 45, 78, 90, 12.9]
print(result1)       # [23, 45, 78, 90, 12.9]
print(result2)      # [23, 45, 78, 90, 12.9]

nums.append(100)
print(nums)         # [23, 45, 78, 90, 12.9, 100]
print(result1)      # [23, 45, 78, 90, 12.9]
print(result2)      # [23, 45, 78, 90, 12.9, 100]

Exercise: delete all failed scores from the list

scores = [90, 45, 34, 89, 76, 23, 100, 23, 6, 59]
# new_scores = scores.copy()
for x in scores[:]:
    print('x:', x)
    if x < 60:
        scores.remove(x)
print(scores)       # [90, 34, 89, 76, 100, 6]

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

nums = [10, 30, 98, 78, 10, 30, 20, 30]
result = nums.count(10)
print(result)
print(nums.count(30))
print(nums.count(100))

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

nums = [10, 20, 30]
nums.append('abc')
print(nums)     # [10, 20, 30, 'abc']

nums.extend('abc')
print(nums)     # [10, 20, 30, 'abc', 'a', 'b', 'c']

nums.append([100, 200])
print(nums)     # [10, 20, 30, 'abc', 'a', 'b', 'c', [100, 200]]

nums.extend([100, 200])
print(nums)

5. List. Index (element) - get the subscript value of the specified element in the list (subscript value starting from 0)

nums = [23, 45, 78, 45, 90, 12.9]
print(nums.index(78))       # 2
# print(nums.index(100))    # report errors
print(nums.index(45))       # 1

6. List. reverse() - reverse the list

nums = [23, 45, 78, 45, 90, 12.9]
nums.reverse()
print(nums)         # [12.9, 90, 45, 78, 45, 23]

7. List. sort() - sort the elements in the list from small to large

List. sort(reverse=True) - sorts the elements in the list from large to small

nums = [23, 45, 78, 45, 90, 12.9]
result = nums.sort()
print(result)       # None
print(nums)     # [12.9, 23, 45, 45, 78, 90]

nums = [23, 45, 78, 45, 90, 12.9]
result = sorted(nums)
print(result)       # [12.9, 23, 45, 45, 78, 90]
print(nums)         # [23, 45, 78, 45, 90, 12.9]

List derivation

1. List derivation - an expression that quickly creates a list

**1) * * structure 1 - uniformly transform the elements in the sequence to get a new list
[expression for variable in sequence]

result = [10 for x in range(4)]
print(result)       # [10, 10, 10, 10]

result = [x*2 for x in range(4)]
print(result)    # [0, 2, 4, 6]

nums = [56, 78, 92, 35, 50]
result = [x % 10 for x in nums]
print(result)    # [6, 8, 2, 5, 0]

Exercise 1: using the list derivation, extract the ten digits of the elements in the list

nums = [56, 738, 92, 351, 50]     # [5, 3, 9, 5, 5]
result = [x // 10 % 10 for x in nums]
print(result)       # [5, 3, 9, 5, 5]

2) Structure II - screening and transformation
[expression for variable in sequence if conditional statement]

result = [x*2 for x in range(5) if x % 2]
print(result)    # [2, 6]

Exercise 1: extract all even numbers from nums using list derivation

# [34, 56, 12]
nums = [19, 34, 56, 77, 9, 12]
result = [x for x in nums if x % 2 == 0]
print(result)   # [34, 56, 12]

Exercise 2: extract all numbers in nums by using list derivation

# [12, 2.34, 9]
nums = [12, 2.34, 'abc', 9, '009']
result = [x for x in nums if type(x) in [int, float]]
print(result)   # [12, 2.34, 9]

Exercise 3: use the list derivation to extract the single digits of all odd numbers in nums

# [9, 7, 1]
nums = [19, 34, 56, 77, 91, 12]
result = [x % 10 for x in nums if x % 2 == 1]
print(result)       # [9, 7, 1]

Exercise 4: use the derivation to divide all the even numbers in the list by 2, and the odd numbers remain the same

# [19, 17, 28, 77, 91, 6]
nums = [19, 34, 56, 77, 91, 12]
# x , x // 2
result = [x if x % 2 else x//2 for x in nums]
print(result)       # [19, 17, 28, 77, 91, 6]

Exercise 5: divide all the numbers in the list by 10. If they are not numbers, don't move

# [3.4, 0.345, 'abc', 8.9, '12', 0.9]
list1 = [34, 3.45, 'abc', 89, '12', 9]

# x / 10, x
result = [x / 10 if type(x) in (int, float) else x for x in list1]
print(result)       # [3.4, 0.34500000000000003, 'abc', 8.9, '12', 0.9]

Keywords: Python

Added by burzvingion on Mon, 18 Oct 2021 23:19:02 +0300