如何在python中循环使用类似名称的函数?

时间:2017-04-18 17:05:24

标签: python function loops

如果我的名字越来越多,我有不同的功能,我该怎么循环呢?例如:

<div id="email-html">
  <p>test ©</p>
</div>

<a id="clickMe" href="#">Click to Download</a>

如何使用:

循环播放它们
def Func1():
    something something

def Func2():
    something something

def Func3():
    something something

...

def Func456832():
    something something

def Func456833():
    something something

本质上是这个问题: How do I loop through functions with a for loop?

编辑:因为有这么多人问,这是我的真实代码:

for i in range(1,456833):

我意识到这是完成它的一种非常糟糕的方式,特别是如果我想继续扩展列表。所以我去寻找答案,这似乎是我需要知道的,以便解决它。

Edit2:我从哪里获得self.distanceX:

def write(self):
    with open('distances.txt', 'a') as file:
        file.write('\n'+str(self.distance1))
        file.write('\n'+str(self.distance2))
        file.write('\n'+str(self.distance3))
        file.write('\n'+str(self.distance4))
        file.write('\n'+str(self.distance5))
        file.write('\n'+str(self.distance6))
        file.write('\n'+str(self.distance7))
        file.write('\n'+str(self.distance8))
        file.write('\n'+str(self.distance9))
        file.write('\n'+str(self.distance10))
        file.write('\n'+str(self.distance11))
        file.write('\n'+str(self.distance12))
        file.write('\n'+str(self.distance13))
        file.write('\n'+str(self.distance14))
        file.write('\n'+str(self.distance15))

编辑:已解决。谢谢大家:)

3 个答案:

答案 0 :(得分:1)

您需要以更好的方式存储所有这些距离。

class NeuralNetwork():
    def __init__(self, inputs, hidden1, hidden2, hidden3, outputs, alpha,it_1,it_2,it_3,it_4,it_5):
        ....
        self.distance1 = [alpha,hidden1,it_1,'train',0,0,0,0,0]
        self.distance2 = [alpha,hidden1,it_1,'test',0,0,0,0,0]
        self.distance3 = [alpha,hidden1,it_1,'dist',0,0,0,0,0]
        ...

我希望可以通过列表将其压缩成:

class NeuralNetwork():
    def __init__(self, inputs, hidden1, hidden2, hidden3, outputs, alpha,it_1,it_2,it_3,it_4,it_5):
        ....
        self.distances = []
        self.distances.append([alpha,hidden1,it_1,'train',0,0,0,0,0])
        self.distances.append([alpha,hidden1,it_1,'test',0,0,0,0,0])
        self.distances.append([alpha,hidden1,it_1,'dist',0,0,0,0,0])
        ...

或更好:

class NeuralNetwork():
    def __init__(self, inputs, hidden1, hidden2, hidden3, outputs, alpha, *iterations):
        ....
        self.distances = []
        for iteration in interations:
            for type in ['train', 'test', 'dist']:
                self.distances.append([alpha, hidden1, iteration, type, 0, 0, 0, 0, 0])

据推测,假设您有50万个条目,还有其他变量,但它们的想法相同:将它们循环到列表中。

然后写成:

def write(self):
    with open('distances.txt', 'a') as file:
        for distance in self.distances:
            file.write('\n'+str(distance))

答案 1 :(得分:0)

假设循环位于同一模块中,则函数位于globals()

for i in range(1, 456834):
    globals()["Func{}".format(i)]()

如果功能号码不连续,您可以过滤命名空间然后调用

import re

for fctn in sorted(obj for name,obj in globals().items() if re.match(r'Func\d+$' name)):
    fctn()

答案 2 :(得分:0)

我假设您正在从同一个文件进行迭代,如果是这样的话......

def a():
    pass

def b():    
    pass

g = globals()

g = g.items()

for i, symbol  in  g:
    if callable(symbol):
        print(i, symbol)

此输出

('a', <function a at 0x107516c08>)
('b', <function b at 0x1075180c8>)