仅用于循环列表中的第二项.(蟒蛇)

Jac*_*xel 5 python for-loop coding-style nested-lists

是)我有的

是这样的

def mymethod():
    return [[1,2,3,4],
            [1,2,3,4],
            [1,2,3,4],
            [1,2,3,4]]

mylist = mymethod()

for _, thing, _, _ in mylist:
    print thing

# this bit is meant to be outside the for loop, 
# I mean it to represent the last value thing was in the for
if thing:
    print thing
Run Code Online (Sandbox Code Playgroud)

我想要的是

我想要做的是避免虚拟变量,是否有更聪明的方法来做到这一点

for thing in mylist:
    print thing[1]
Run Code Online (Sandbox Code Playgroud)

因为那时我将不得不使用thing[1]任何其他时间我需要它,而不是将它分配给一个新的变量然后事情变得凌乱.

如果我错过了一些明显的东西,那么很抱歉

Kie*_*ong 7

你可以破解生成器表达式

def mymethod():
    return [[1,2,3,4],
            [1,2,3,4],
            [1,2,3,4],
            [1,2,3,4]]

mylist = mymethod()

for thing in (i[1] for i in mylist):
    print thing

# this bit is meant to be outside the for loop, 
# I mean it to represent the last value thing was in the for
if thing:
    print thing
Run Code Online (Sandbox Code Playgroud)