如何从左到右解包元组? [英] How to unpack a tuple from left to right?

查看:32
本文介绍了如何从左到右解包元组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种干净/简单的方法可以将右侧的 Python 元组从左到右解包?

Is there a clean/simple way to unpack a Python tuple on the right hand side from left to right?

例如

j = 1,2,3,4,5,6,7

(1,2,3,4,5,6,7)

v,b,n = j[4:7] 

我可以修改切片符号,使 v = j[6], b=j[5], n=j[4] 吗?

Can I modify the slice notation so that v = j[6], b=j[5], n=j[4] ?

我意识到我可以只订购左侧以获得所需的元素,但在某些情况下,我想我只想从左到右解压缩元组.

I realise I can just order the left side to get the desired element but there might be instances where I would just want to unpack the tuple from left to right I think.

推荐答案

如果您想保留原始索引(即不想费心将 4 和 7 更改为 6 和 3),您还可以使用:

In case you want to keep the original indices (i.e. don't want to bother with changing 4 and 7 to 6 and 3) you can also use:

v, b, n = (j[4:7][::-1])

这篇关于如何从左到右解包元组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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