Skip to main content
 首页 » 编程设计

python之使用尾元组的变量迭代元组列表

2024年11月24日7unruledboy

<分区>

如何迭代 [(1,2,3), (2,3,1), (1,1,1)] 并将每个元组拆分为头和尾? 我正在寻找像这样简洁的 pythonic 方式:

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。