python 基础之列表生成式

# 列表生成式 #

# ```python

# 列表生成式:

可以快速方便地生成列表 print([i for i in range(1, 11)])#[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

print([i for i in 'abcdefg'])#['a', 'b', 'c', 'd', 'e', 'f', 'g']

print([i * 2 for i in range(1, 11)])#[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

print([i * i for i in range(1, 11)])#[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

print([str(i) for i in range(1, 11)])#['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']

print([i for i in range(1, 11) if i % 2 == 0])#[2, 4, 6, 8, 10] # 生成的列表可以与遍历的内容没有一毛钱关系

print([250 for i in range(1, 11)])#[250, 250, 250, 250, 250, 250, 250, 250, 250, 250]

posted @ 2018-12-19 21:44  青春叛逆者  阅读(178)  评论(0编辑  收藏  举报