Python - List_Comprehension(列表解析式)

版权声明:转载请注明出处 https://blog.csdn.net/qq_42292831/article/details/89338389

a = [1,2,3,4,5,6,7,8,9]

我们要对a中的每个元素取平方:

1> square = [i ** 2 for i in a]

2> square = list(map(lambda x: x ** 2, a))

[ 一般情况下,使用列表解析式会比使用一些高阶函数更加简洁和容易理解 ]



from pprint import pprint

number = range(3)
a = [str(i)+"***"+str(j) for i in number for j in number]
x,y,z = [list(i) for i in "ABC"]
pprint(a)
print(x,y,z)



list.append(value)

list.pop(index)

list.pop()

list.remove(value) 


a = sorted(Data,key=lambda x:x[1])

a = sorted(Data)

a = reversed(Data)



列表的快速初始化:

a = [0]  * 10

>>> [0,0,0,0,0,0,0,0,0,0]

猜你喜欢

转载自blog.csdn.net/qq_42292831/article/details/89338389