Day-7 list and derivation of list

Posted by prc on Wed, 08 Dec 2021 07:28:48 +0100

01. List related operators

1. Mathematical operators: +*

List 1 + list 2 - combine the elements of the two lists to produce a new list

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

List * n - 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]

Comparison operators: >, <, > =, < =, = ==**

Add: different types can use = = 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}=={10,20,30})   #True (collection out of order)

#2)>,<,>=,<=
The two lists compare the size of the first pair of unequal elements

print([10,100,200,300]>[20,1])					#False
print([10,100,200,300]>[10,20,1000000,800])		#True
print([10,20,30]>[10,20])						#True

02. List related functions

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

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

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=[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]

4.len
Len (sequence) - gets the number of elements in the sequence

print(len([10,20,30]))      #3
print(len(['abc123']))      #1
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]

03. List related methods

1. List. clear() - clear the list

nums=[1,56,565,459,3]
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=[1,56,565,459,3]
new_nums1=nums.copy()
print(new_nums1) #[1,56,565,459,3]

#When a variable saves data, it actually saves the address of the data in memory (all variables in python are pointer variables)
#One variable directly assigns an address to another variable

new_nums2=nums
print(new_nums2)

print('nums of id',id(nums))               #Num's id 2370805777024
print('new_nums1 of id',id(new_nums1))     #ID 23708544288 of new_nums1
print('new_nums2 of id',id(new_nums2))     #ID 23708577024 of new_nums2

nums.append(100)
print(new_nums1)                        #[1, 56, 565, 459, 3]
print(new_nums2)                        #[1, 56, 565, 459, 3, 100]

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

c1=nums.count(5)
print(c1)           #0

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

nums=[100,200]

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 there are multiple elements, the subscript of the first one will be returned; if the element does not exist, an error will be reported

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)
print(result)   #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]

7. Sorting
List. sort() / list. sort(reverse=True)
Sorted (sequence) / 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

04. List derivation

List derivation - format for quickly creating lists

1. Derivation structure 1

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

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

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

Exercise: 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]
list1=[x%10 for x in nums]
print(list1)

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]
list1=[x*10 for x in nums]
print(list1)

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 ',' pass']
[True, True, False, True, False, True]

scores = [90, 78, 45, 67, 39, 89]
list1=[x>=60 for x in scores]
print(list1)

result=['pass'if x>=60 else 'fail,' for x in scores]
print(result)

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]
result=[int(x/2) if x%2==0 else x*2 for x in nums]
print(result)

2. Derivation structure 2

[expression for variable in sequence if conditional statement]
Create a list and variable to take values from the sequence one by one. Until the value is taken, judge whether the conditional statement is True for each value. If it is True, the result of the calculation expression is the element of the list; if not, continue to take the next one

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

Exercise 1: extract all odd numbers in nums using list derivation
#[23, 89, 41]

nums = [23, 89, 90, 56, 41, 802]
result=[x for x in nums if x%2]
print(result)   #[23, 89, 41]

Exercise 2: given a list, extract all strings in the list

list1 = [10, 23.9, 'hello', True, '12', 0]
result=[x for x in list1 if type(x)==str]
print(result)

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]
result=[x+3 for x in list1 if type(x) ==int]
print(result)

05. Supplementary three item operator

C and Java:
Expression? Value 1: value 2 - if the value of the expression is true, the result is value 1, otherwise the result is value 2

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

06. Yuanzu

Yuanzu is an immutable list
Query, in and not in, mathematical operation, comparison operation, correlation function - Yuanzu are all supported

1. What is tuple

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

Kong Yuanzu

t1=()
print(t1,type(t1))      #() <class 'tuple'>

The ancestor of an element

t2=(10,)
print(t2,type(t2))      #(10,) <class 'tuple'>

t3=(12,56,78,96,42,36,72)
print(t3[1])        #56
print(t3[-1])       #72
print(t3[-3:])      #(42, 36, 72)

Topics: Python Algorithm