25 lines
467 B
Python
25 lines
467 B
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
"""
|
|
@File : demo.py
|
|
@Author : lichao
|
|
@Date : 2024/11/27 15:40
|
|
@Software : PyCharm
|
|
"""
|
|
|
|
# 列表推导式
|
|
list = [x for x in range(1, 15)]
|
|
list = [list[x : x + 3] for x in range(0, len(list), 3)]
|
|
print(list)
|
|
|
|
|
|
# 集合推导式
|
|
gather = {x for x in range(1, 10)}
|
|
print(gather) # {1, 2, 3, 4, 5, 6, 7, 8, 9}
|
|
|
|
# 字典推导式
|
|
dict = {x: x**2 for x in range(1, 10)}
|
|
print(dict)
|
|
|
|
# 没有元组推导式
|