Python从入门到高手(内部资源-配学习资料)_哔哩哔哩_bilibili
# 列表推导式 字典推导式 集合推导式
# 旧的列表---新的列表
# 1. 列表推导式 格式 [表达式 for 变量 in 旧列表] 或者 [表达式 for 变量 in 旧列表 if 条件]
# 过滤掉长度小于或等于3的人名
# names = ['tom', 'lily', 'abc', 'jack', 'steven', 'bob']
# result = [name for name in names if len(name) > 3]
# print(result)
#
# result = [name.capitalize() for name in names if len(name) > 3]
# print(result)
#
# # 将1-100之间能被3整除,组成一个新的列表
# newlist = [i for i in range(1, 101) if i % 3 == 0 and i % 5 == 0]
# print(newlist)
#
# # 0~5偶数 0~10奇数
# newlist = [(x, y) for x in range(5) if x % 2 == 0 for y in range(10) if y % 2 != 0]
# print(newlist)
#
# # list={[1,2,3],[4,5,6],[7,8,9],[1,3,5]}---[3,6,9,5]
# list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 3, 5]]
# newlist = [i[2] for i in list1]
# print(newlist)
dict1 = {'name': 'tom', 'salary': 5000}
dict2 = {'name': 'lucy', 'salary': 8000}
dict3 = {'name': 'jack', 'salary': 4500}
dict4 = {'name': 'lily', 'salary': 3000}
list1 = [dict1, dict2, dict3, dict4]
# if薪资大于5000加200,低于等于5000加500
newlist = [employee['salary'] + 200 if employee['salary'] > 5000 else employee['salary'] + 500 for employee in list1]
print(newlist)
# [(偶数,奇数),(),(),()]
''':
def func(name):
newlist=[]
for name in names:
if len(name)>3:
name=name.title()
newlist.append(name)
return newlist
def func():
newlist=[]
for i in range(5): # 偶数
if i%2==0:
for j in range(10):
if j%2!=0:
newlist.append((i,j))
return newlist
'''
|