python3 lambda 掉坑里

大叔的python之路 andy 5年前 (2019-06-16) 3530次浏览 已收录 0个评论 扫描二维码

python3 lambda 掉坑里

price_float_array = [float(price_str) for price_str in stock_dict.values()]
print(price_float_array)

pp_array = [(price1, price2) for price1, price2 in zip(price_float_array[:-1], price_float_array[1:])]
print(pp_array)

chang_array = map(lambda pp: reduce(lambda first_price, second_price: round((second_price - first_price)/first_price, 3), pp), pp_array)

res = list(chang_array)
#res = chang_array
res.insert(0, 0)
print(res)

stock_namedtuple = namedtuple('stock', ('date', 'price', 'change'))
stock_dict = OrderedDict((date, stock_namedtuple(date, price, change)) for date, price, change in zip(date_array, price_array, res))
print('stock')
print(stock_namedtuple)

new_list = [stock_namedtuple(date, price, change) for date, price, change in zip(date_array, price_array, res)]
print(new_list)
'''
print(stock_namedtuple(date, price, change) for date, price, change in zip(date_array, price_array, chang_array))
'''
print(len(stock_dict))

从python2 换成 3 每次都提醒自已,别掉坑里了
这次还是掉入 lambda 不执行的问题
上面 第 7 行 chane_array 还是未运行的, 第9行 res 才是已经运行过的结果(後来第11行又 res.insert(0, 0))

导致下面第15行 OrderedDict内的 zip 要放入 res 而不是 change_array

惨..


神隊友學長Andy , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:python3 lambda 掉坑里
喜欢 (0)
[[email protected]]
分享 (0)
andy
关于作者:
中年大叔,打拼 like young students.
发表我的评论
取消评论
表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址