如何在特定条件下迭代元组函数

时间:2016-06-14 13:45:51

标签: python

我想按照以下某些条件迭代元组。 我已经定义了一个变量L = []。我有几个函数导入元组,我想,如果满足这个条件,迭代通过元组,满足这个条件的函数将打印出答案。我已经制定了这个代码但是看不到正确的方法。我不是一个专业的蟒蛇大师,我欢迎任何评论家。

L = []
def one():    
    L = 1*2    
    print '1 yea!'
    return L

def two():
    L = 1+2    
    print '2 yea!'
    return L

def three():
    L = 1/2    
    print ' 3 yea!'
    return L

def four ():
    L = 1-2    
    print '4 yea!'
    return L

refined = (one, two, three, four)

def these():
    for x in refined:
        b1 = iter(refined)
        if L ==2:
            return b1
        else:
            print 'nothing here'

1 个答案:

答案 0 :(得分:0)

好的,

def one():    
    L = 1*2    
    print '1 yea!'
    return L

def two():
    L = 1+2    
    print '2 yea!'
    return L

def three():
    L = 1/2    
    print ' 3 yea!'
    return L

def four ():
    L = 1-2    
    print '4 yea!'
    return L

refined = (one, two, three, four)

def these(refined, L):
    return refined[L]

for L in range(4):
    print these(refined, L)()

编辑:1个特定值:

print these(refined, 2)()

运行并获取:

$ python refined.py
1 yea!
2
2 yea!
3
 3 yea!
0
4 yea!
-1