之前一个不小心把之前的全部删除了,也没有办法回复,所以以后还是在博客上记录一下
name = ['alex', 'wupeiqi', 'yuanhao', 'nezha']
def func(item):
return item + '_sb'
ret = map(func, name)
for i in ret:
print(i)
print(list(i)) # list把字符串拆开变为每一个元素
print(list(ret)) # ret为迭代器,取完后就不含有元素,返回一个空列表
ret = map(lambda item: item+'_sb', name) # lambda与函数无关
print(list(ret))
print(list(ret)) # ret迭代器,取完后依旧消失
num = [1, 3, 5, 7, 8]
def func1(x):
if x % 2 == 0:
return True # return x % 2== 0
ret = filter(func1, num) # func和可迭代的 filter返回值为迭代器
print(list(ret))
ret = filter(lambda x: True if x % 2 == 0 else False, num) # 冒号后面可以写表达式和for循环
with open('C:/Users/dell/Desktop/test20.txt', encoding='utf-8') as f:
p = f.readlines()
page_num = int(input('输入页码:'))
pages, mod = divmod(len(p), 5) # 商余函数,返回两个值
if mod: # mod不为零
pages += 1
if page_num > pages:
print('error')
elif page_num == pages and mod != 0:
for i in range(mod):
print(p[(page_num-1)5 + i].strip()) # 取消输出换行
else:
for i in range(5):
print(p[(page_num-1)5 + i].strip())
portfolio = [
{'name': 'IBM', 'shares': 100, 'price': 91.1},
{'name': 'AAPL', 'shares': 50, 'price': 543.22},
{'name': 'FB', 'shares': 200, 'price': 21.09},
{'name': 'HPQ', 'shares': 35, 'price': 31.75},
{'name': 'YHOO', 'shares': 45, 'price': 16.35},
{'name': 'ACME', 'shares': 75, 'price': 115.65}
]
ret = map(lambda dic: {dic['name']: round(
dic['share']*dic['price'], 2)}, portfolio) # round 精确小数位
print(list(ret))
ret = filter(lambda dic: True if dic['price'] > 100 else False, portfolio)
ret = filter(lambda dic: dic['price'] > 100, portfolio)
print(ret)
###########################################################################################