作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我要转换 list = [1,4,2,3,0]
至 list_tup = [(1,4),(4,2),(2,3),(3,0)]
.你可以在下面看到我的代码,但它输出 [(1,4),(2,3)]
.我想知道如何调整 zip 中的索引。
list=[1,4,2,3,0]
list_tup = tuple(zip(list[0::2], list[1::2]))
最佳答案
尝试使用没有第一个元素的列表压缩整个列表:
l = [1,4,2,3,0]
print(list(zip(l, l[1:])))
或者使用解包
*
:
l = [1,4,2,3,0]
print([*zip(l, l[1:])])
他们都输出:
[(1, 4), (4, 2), (2, 3), (3, 0)]
关于python - 如何在python中将每对列表转换为元组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65376945/
我是一名优秀的程序员,十分优秀!