当我不知道将要进行多少回合时,进行多次回合

尼古拉斯·雷诺(Nicolas Renaud)

我研究了不同的帖子,似乎真的不建议在python中动态定义变量。

我面临一个问题,我想分析多个回合(回合的数目可以变化),我找到了一种方法来做,但是我发现它有点残酷(它涉及到复制粘贴相同代码的50倍):

def ProcessRound(Input1,Input2,Input3,Source1):
    #the function process the Source and assign the result to Inputs with the right name
    return Input1,Input2,Input3

StopProcess = 0

Source1 = "Text I want to analyse that may exist or not if doesnt exist it is False"

if Source1 != False:
    Input1Rnd1 = []
    Input2Rnd1 = []
    ProcessRound(Input1Rnd1,Input2Rnd1,Source1)
else:
    StopProcess = 1
    #if this step not valid there are no more steps so want to avoid continuing
    
if StopProcess !=1:
    Source2 = "Text2"
    if Source2 != False:
        Input1Rnd2 = []
        Input2Rnd2 = []
        ProcessRound(Input1Rnd2,Input2Rnd2,Source2)
    else:
        StopProcess = 1

#there can be many rounds up to 50 but trying to keep question clear

因此,如果我将这些图表复制并改编50次有效(在前5轮中进行了测试),但对我来说听起来有点蛮力?理想情况下,我将更喜欢执行以下操作:

def ProcessRound(i,Sourcei):
    #the function process the Source
    return Input1rndi,Input2rndi,Input3rndi
#here the main difference is the the return variable would be named based on the round number:i

StopProcess = 0
i= 0

while StopProcess != 1:
    Sourcei = "Text I want to analyse where i in the name would change"
    if Sourcei != False:
       LastRoundReached = i
       StopProcess = 1
    else:
       ProcessRound(i,Sourcei)

如果我这样做,我会得到我需要使用的所有变量(input1rnd1,input2rnd1,input1rnd2 ...),但不必复制粘贴我的代码50次

这是我在此网站上提出的第一个问题,我正在尝试遵循适当的准则,但如果您认为这不合适,请立即发表评论

加密傻瓜

确切的说,您仍然想在这里做什么。看来,您遇到的主要问题是编写一个数据驱动的解决方案,在该解决方案中可以编写一次处理代码,然后可以通过定义包含每个回合输入的数据结构来处理任意数量的“回合”。

我在您的示例中收集到的信息,每一轮的输入都是一个字符串。处理该输入,并通过该过程产生三个结果值。

这是一个示例代码,说明解决上述问题的一种干净方法:

sources = [
    "Text I want to analyse that may exist or not if doesnt exist it is False",
    "Text I want to analyse where i in the name would change",
    "Text2"
]

results = []

def ProcessRound(i, source):
    # the function processes the provided source, resulting in three output values
    result1 = f"<result data for round {i}>"
    result2 = f"<more result data for round {i}>"
    result3 = f"<even more result data for round {i}>"
    return result1, result2, result3

# Process each source in turn...
# "enumerate" gives us an index value for each iteration over our list so
# that we don't have to manage an index variable ourself.
for i, source in enumerate(sources):

    # Process the next source value and store the results in our 'results' list
    results.append((source, ProcessRound(i, source)))

# ... later on, process the results.  If you wanted to do this processing
# at another time, you could store the results in a database instead of an
# in-memory list.  That is left as an exercise for the OP :)
for i, result in enumerate(results):

    # Break out the stored result
    source, (result1, result2, result3) = result

    # Here you would process the results of the round however you want.  As an example,
    # here we just print all of the information we have about the round, including the result
    print(f"Result of round {i}:")
    print("   Input:", source)
    print("   Output:", result1, result2, result3)

结果:

Result of round 0:
   Input: Text I want to analyse that may exist or not if doesnt exist it is False
   Output: <result data for round 0> <more result data for round 0> <even more result data for round 0>
Result of round 1:
   Input: Text I want to analyse where i in the name would change
   Output: <result data for round 1> <more result data for round 1> <even more result data for round 1>
Result of round 2:
   Input: Text2
   Output: <result data for round 2> <more result data for round 2> <even more result data for round 2>

更新:我根据OP希望存储处理结果的要求更新了答案,然后在以后使用这些结果。请注意,我将原始数据存储source在每个结果记录中,以便以后可以直接运行与原始答案完全相同的处理代码,而无需进行任何更改。因此结果是相同的。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何进行CardView回合

来自分类Dev

使用XStream读取XML进行对象处理时,如果我不知道XML将具有多少个字段怎么办?

来自分类Dev

如何进行数字回合?

来自分类Dev

使用Sinatra进行的比分和回合

来自分类Dev

compareTo正在要求我进行投射,但我不知道为什么

来自分类Dev

我需要使用ArrayList类对数组进行排列。我不知道怎么了

来自分类Dev

当我不知道签名时如何调用函数?

来自分类Dev

std :: sort函数未对向量进行排序,我也不知道为什么?

来自分类Dev

我可以在不知道动态数组大小的情况下对其进行迭代

来自分类Dev

在不知道ID的情况下进行更新

来自分类Dev

Java Clock模拟,不知道如何进行...

来自分类Dev

不知道如何使用外部窗格进行GUI

来自分类Dev

在不知道ID的情况下进行更新

来自分类Dev

我不知道这个

来自分类Dev

我不知道的结果

来自分类Dev

我正在尝试进行此sql更新查询,但我不知道为什么它不起作用

来自分类Dev

当我不知道其中有多少个文本框时,应该如何处理多个文本框?

来自分类Dev

代码效果很好,但是我不知道如何在第二步中进行逆向处理

来自分类Dev

使用.sort对数字数组进行排序,但我不知道为什么这样做

来自分类Dev

不确定是否可以过滤我要分析的数据,不知道是否可以在excel中进行

来自分类Dev

如何对 Array[Array[Int]] 进行排序,假设内部数组具有相同的大小,但我们不知道

来自分类Dev

DES程序进行了很多回合。

来自分类Dev

从一个准备好的语句中进行许多php mysqli查询,却不知道有多少个参数

来自分类Dev

如果我不知道每秒有多少次运行,那么如何获得相同的输出?

来自分类Dev

如果我不知道加密方法和参数,加密强度是多少?

来自分类Dev

如果我不知道深度是多少,如何在 Javascript 中动态处理嵌套对象?

来自分类Dev

C ++当我不知道子类型时,从基类中调用子类函数

来自分类Dev

当我不知道显式链接时,如何通过终端下载文件?

来自分类Dev

在PHP中,当我不知道子对象的名称时该如何访问?

Related 相关文章

  1. 1

    如何进行CardView回合

  2. 2

    使用XStream读取XML进行对象处理时,如果我不知道XML将具有多少个字段怎么办?

  3. 3

    如何进行数字回合?

  4. 4

    使用Sinatra进行的比分和回合

  5. 5

    compareTo正在要求我进行投射,但我不知道为什么

  6. 6

    我需要使用ArrayList类对数组进行排列。我不知道怎么了

  7. 7

    当我不知道签名时如何调用函数?

  8. 8

    std :: sort函数未对向量进行排序,我也不知道为什么?

  9. 9

    我可以在不知道动态数组大小的情况下对其进行迭代

  10. 10

    在不知道ID的情况下进行更新

  11. 11

    Java Clock模拟,不知道如何进行...

  12. 12

    不知道如何使用外部窗格进行GUI

  13. 13

    在不知道ID的情况下进行更新

  14. 14

    我不知道这个

  15. 15

    我不知道的结果

  16. 16

    我正在尝试进行此sql更新查询,但我不知道为什么它不起作用

  17. 17

    当我不知道其中有多少个文本框时,应该如何处理多个文本框?

  18. 18

    代码效果很好,但是我不知道如何在第二步中进行逆向处理

  19. 19

    使用.sort对数字数组进行排序,但我不知道为什么这样做

  20. 20

    不确定是否可以过滤我要分析的数据,不知道是否可以在excel中进行

  21. 21

    如何对 Array[Array[Int]] 进行排序,假设内部数组具有相同的大小,但我们不知道

  22. 22

    DES程序进行了很多回合。

  23. 23

    从一个准备好的语句中进行许多php mysqli查询,却不知道有多少个参数

  24. 24

    如果我不知道每秒有多少次运行,那么如何获得相同的输出?

  25. 25

    如果我不知道加密方法和参数,加密强度是多少?

  26. 26

    如果我不知道深度是多少,如何在 Javascript 中动态处理嵌套对象?

  27. 27

    C ++当我不知道子类型时,从基类中调用子类函数

  28. 28

    当我不知道显式链接时,如何通过终端下载文件?

  29. 29

    在PHP中,当我不知道子对象的名称时该如何访问?

热门标签

归档