Python list comprehension application

1. The official tutorial address of list comprehension:
https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

2. Use summary:
[Expression for iterative variable in iterable object [if conditional expression]]

3. My use
Insert picture description here
Code A:
Create a list, for loop to take list information and insert information

------------------------------A
a =[]

for element in title[:3]:#把所有信息全部打印出来,但是只要前3个
    a.append(element.text)
print(a)

Create a list, the for loop takes the list information and inserts the information

代码-----------------B
#把前3列存入一个列表
# texts = [f.text for f in title[:3]]

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_41665637/article/details/112261359