Python 使用尾部元组的变量迭代元组列表

Python 使用尾部元组的变量迭代元组列表,python,python-2.7,for-loop,tuples,Python,Python 2.7,For Loop,Tuples,如何迭代[(1,2,3),(2,3,1),(1,1,1)],同时将每个元组拆分为头和尾? 我正在寻找一种类似于蟒蛇的方式: for h, *t in [(1,2,3), (2,3,1), (1,1,1)]: # where I want t to be a tuple consisting of the last two elements val = some_fun(h) another_fun(val, t) 上述内容不适用于python 2.7。您可以使用映射到和

如何迭代
[(1,2,3),(2,3,1),(1,1,1)]
,同时将每个元组拆分为头和尾? 我正在寻找一种类似于蟒蛇的方式:

for h, *t in [(1,2,3), (2,3,1), (1,1,1)]:
    # where I want t to be a tuple consisting of the last two elements
    val = some_fun(h)
    another_fun(val, t)

上述内容不适用于python 2.7。

您可以使用
映射到和列表切片:

for h, t in map(lambda x: (x[0], x[1:]), [(1,2,3), (2,3,1), (1,1,1)]):
    print("h = %s, t=%s"%(h, t))
#h = 1, t=(2, 3)
#h = 2, t=(3, 1)
#h = 1, t=(1, 1)

尝试用括号括起
h,*t
,例如,
(h,*t)
@dcg,这也不起作用。据我所知,Python2不支持这种解包。
上述内容不适用于Python2.7。在这里学到了一些新东西。