相乘相邻元素 [英] Multiply Adjacent Elements

查看:62
本文介绍了相乘相邻元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个整数元组,例如(1, 2, 3, 4, 5),我想通过乘以相邻元素来生成元组(1*2, 2*3, 3*4, 4*5).可以用单缸纸做到这一点吗?

I have a tuple of integers such as (1, 2, 3, 4, 5) and I want to produce the tuple (1*2, 2*3, 3*4, 4*5) by multiplying adjacent elements. Is it possible to do this with a one-liner?

推荐答案

又矮又甜.请记住,zip只运行最短输入的时间.

Short and sweet. Remember that zip only runs as long as the shortest input.

print tuple(x*y for x,y in zip(t,t[1:]))

这篇关于相乘相邻元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆