Derivative Encyclopedia of Python Basics

Table of contents

1. Inductive introduction

2. List comprehension

3. Tuple derivation

4. Dictionary derivation

5. Set comprehension


1. Inductive introduction

Python derivation is a unique data processing method that can construct another new data sequence structure from one data sequence.

Python supports derivation of various data structures:

list comprehension

tuple comprehension

dictionary comprehension

set comprehension

2. List comprehension

1. List comprehension, also known as list comprehension

2. Function: a concise creation list

3. Format

Format 1: [expression for variable in iterable (iterable object)]

Format 2: [expression for variable in iterable (iterable object) if condition]

Iterable objects: sequences, strings, tuples, lists

You can put any type of object in the list and the return result will be a new list, where the if and for statements are generated after the contextual expressions are run.

4. The order of list comprehension execution: progressive from left to right, and the nesting relationship between statements

5. Simple mode: only includes loops and does not include conditional judgments

# 输出1-9列表
list_1 = [x for x in range(1, 10)]
print(list_1)

# 求列表元素+1后的值
list_1 = [x + 1 for x in range(1, 10)]
print(list_1)

list_2 = [x * x for x in range(1, 10)]
print(list_2)

# 包括判断和筛选
list_1 = [x for x in range(1, 10) if x % 2 == 0]
print(list_1)

list_2 = [x * 10 for x in range(1, 10) if x % 2 == 0]
print(list_2)

# 包括循环嵌套和条件筛选
list_1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list_2 = [x for y in list_1 for x in y if x % 2 == 0]
print(list_2)

# 遍历字符串
list1 = [x for x in 'python']
print(list1)

# 双遍历字符相加
list2 = [x + y for x in 'python' for y in '12']
print(list2)

3. Tuple derivation

1. The tuple derivation formula can use data types such as range intervals, tuples, lists, dictionaries, and sets to quickly generate a tuple that meets the specified requirements.

2. Format:

Format 1: tuple (expression for variable in iterable (iterable object))

Format 2: tuple (expression for variable in iterable (iterable object) if condition)

case:

tup1 = (x for x in range(1, 10))
print(tup1)  # 返回生成器对象

tup2 = tuple(x for x in range(1, 10))
print(tup2)

4. Dictionary derivation

The format of the dictionary comprehension is similar to that of the list comprehension, the difference is that the type of the return value of the dictionary comprehension is a dictionary.

Format:

Format 1: {key : value for value in iterable (iterable object)}

Format 2: {key : value for value in iterable (iterable object) if condition}

dict1 = {x: x ** 2 for x in range(0, 9)}
print(dict1)

list1 = ['hello', 'python', 'hhhhhh']
dict1 = {key: len(key) for key in list1}
print(dict1)

5. Set comprehension

1. The collection is unordered and non-repetitive

2. Format:

Format 1: {expression for variable in iterable (iterable object)}

Format 2: {expression for variable in iterable (iterable object) if condition}

set1 = {x for x in 'abbccdef12345'}
print(set1)
print(type(set1))

Guess you like

Origin blog.csdn.net/xiao__dashen/article/details/125227810