Python common operations and Derivations

Python common operations and Derivations

1, Public operation

  • operator

  • Public method

  • Container type conversion

1, Operator

operator describe Supported container types
+ merge String, list, tuple
* copy String, list, tuple
in Does the element exist String, list, tuple, dictionary
not in Does the element not exist String, list, tuple, dictionary

1.1 +

#1. String
str1='aa'
str2='bb'
str3=str1+str2
print(str3)
#aabb

# 2. List
list1=[1,2]
list2=[10,20]
list3=list1+list2
print(list3)
#[1, 2, 10, 20]

# 3. Tuple
t1=(1,2)
t2=(10,20)
t3=t1+t2
print(t3)
#(1, 2, 10, 20)

1.2 *

#1. String
print('-'*10)

# 2. List
list1=['hello']
print(list1*4)

# 3. Tuple
t1=('world',)
print(t1*4)

1.3 in or not in

#1. String
print('a' in 'abcd')
print('a' not in 'abcd')

# 2. List
list1=['a','b','c''d']
print('a' in list1)
print('a' not in list1)

# 3. Tuple
t1=('a','b','c','d')
print('aa' in t1)
print('aa' not in t1)

2, Public method

function describe
len() Calculate the number of elements in the container
Del or del() delete
max() Returns the maximum value of the element in the container
min() However, the minimum value of the element in the container will be
range(start,end,step) Generate the number from star to end in step for the for loop
enumerate() The function is used to combine a traversable data object (such as list, tuple or string) into an index sequence, and list data and data subscripts at the same time. It is generally used in the for loop

2.1 len()

#1. String
str1='abcdefg'
print(len(str1))

# 2. List
list1=[10,20,30,40]
print(len(list1))

# 3. Tuple
t1=(10,20,30,40)
print(len(t1))

# 4. Assemble
s1={10,20,30}
print(len(s1))

# 5. Dictionary
dict={'name':'Rose','age':18}
print(len(dict))

2.2 del()

#1. String
str1='abcdefg'
del str1
print(str1)

# 2. List
list1=[10,20,30,40]
del(list1[0])
print(list1)
#[20, 30, 40]

2.3 max()

#1. String
str1='abcdefg'
print(max(str1))#g

# 2. List
list1=[10,20,30,40]
print(max(list1))#40

2.4 min()

#1. String
str1='abcdefg'
print(min(str1))#a

# 2. List
list1=[10,20,30,40]
print(min(list1))#10

2.5 range()

for i in range(1,10,1):
    print(i)
#1 2 3 4 5 6 7 8 9

for i in range(1,10,2):
    print(i)
# 1 3 5 7 9

for i in range(10):
    print(i)
#0 1 2 3 4 5 6 7 8 9

2.6 enumerate()

  • grammar
enumerate(Traversable objects, start=0)

Note: the start parameter with is used to set the starting value of the subscript of traversal data, which is 0 by default.

  • experience
list1=['a','b','c','d','e']
for i in enumerate(list1):
    print(i)

for index,char in enumerate(list1,start=1):
    print(f'The subscript is{index},The corresponding character is{char}')

3, Container type conversion

3.1 tuple()

Function: convert a sequence into tuples

list1=[10,20,30,40,50,20]
s1={100,200,300,400,500,}

print(tuple(list1))
print(tuple(s1))

3.2 list()

Function: convert a sequence into a list

t1=('a','b','c','d','e')
s1={100,200,300,400,500}

print(list(t1))
print(list(s1))

3.3 set()

Function: convert a sequence into a set

list1 = [10, 20, 30, 40, 50, 20]
t1 = ('a', 'b', 'c', 'd', 'e')
print(set(list1))
print(set(t1))

5, Derivation

1, List derivation

As: Use an expression to create a regular list or controls a regular list.
The following table derivation formula is called list formation formula

1.1 experience

Create a requirement list of 0-10

  • while loop implementation
#1. Prepare an empty list
list1 = []
# 2. Write cycle, and add numbers to the empty list list1 in turn
i = 0
while i < 10:
    list1.append(i)
    i += 1
print(list1)

  • for loop implementation
list1 = []
for i in range(10):
    list1.append(i)
print(list1)

  • List derivation implementation
list1 = [i for i in range(10)]
print(list1)

1.2 list derivation with if

Requirement: create an even list of 0-10

  • Method 1: range() step implementation
list1 = [i for i in range(0, 10, 2)]
print(list1)

  • Method 2: if implementation
list1 = [i for i in range(10) if i % 2 == 0]
print(list1)

1.3 list derivation of multiple for loops

Requirement creation list is as follows:

[(1, 0), (1, 1), (1, 2), (2, 0), (2, 1), (2, 2)] 
  • The code is as follows:
list1 = [(i, j) for i in range(1, 3) for j in range(3)]
print(list1)

2, Dictionary derivation

Think: if there are two lists

list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, 'man']

How to quickly merge into one dictionary?

Answer: Dictionary derivation

Dictionary derivation function: quickly merge the list or extract the target data in the dictionary

2.1 experience

1. Create a dictionary: the dictionary key is a 1-5 number, and the value is the power of the number

dict1 = {i: i**2 for i in range(1, 5)}
print(dict1) # {1: 1, 2: 4, 3: 9, 4: 16}

2. Merge the two lists into one dictionary

list1 = ['name', 'age', 'gender']
list2 = ['Tom', 20, 'man']
dict1 = {list1[i]: list2[i] for i in range(len(list1))}
print(dict1)

3. Extract target data from dictionary

counts = {'MBP': 268, 'HP': 125, 'DELL': 201, 'Lenovo': 199, 'acer': 99}
# Demand: extract dictionary data with the number of computers above 200 or more
count1 = {key: value for key, value in counts.items() if value >= 200}
print(count1) # {'MBP': 268, 'DELL': 201}

III Set derivation

Requirement: create a set with data to the power of 2 in the list below

list1 = [1, 1, 2] 

The code is as follows:

list1 = [1, 1, 2]
set1 = {i ** 2 for i in list1}
print(set1) # {1, 4}

Note: the collection has the function of data De duplication

4, Summary

  • Function of derivation: simplify code
  • Writing method of derivation
# Derivation of column table
[xx for xx in range()]
# Dictionary derivation
{xx1: xx2 for ... in ...}
# Set derivation
{xx for xx in ...}

Tags: Python

Posted by glitch003 on Thu, 11 Aug 2022 22:28:18 +0530