我研究了不同的帖子,似乎真的不建议在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] 删除。
我来说两句