简体   繁体   English

Python:循环中的if else语句

[英]Python : loop in if else statement in a loop

For each iteration in given script, i am importing a data and computing a variable. 对于给定脚本中的每个迭代,我正在导入数据并计算变量。 for simplicity, i am not including all that stuff and lets say i want to print the ID for each of 25 cases. 为简单起见,我不包括所有这些内容,可以说我想为25种情况中的每一种打印ID。 In whatever way, i place the last statement, it always prints - either all iterations for last data or last iteration for all data. 无论采用哪种方式,我都会放置最后一条语句,它始终会打印-最后一条数据的所有迭代或所有数据的最后一个迭代。

for data in range(0,5):

    if data==0:
        for iteration in range(0,5):
            # Import some data 
            ID = (data,iteration)

    elif data ==1:
        for iteration in range(0,5):
            # Import some data 
            ID = (data,iteration)

    elif data ==2:
        for iteration in range(0,5):
            # Import some data 
            ID = (data,iteration)

    elif data ==3:
        for iteration in range(0,5):
            # Import some data 
            ID = (data,iteration)           

   elif data ==4:
        for iteration in range(0,5):
            # Import some data 
            ID = (data,iteration)


   ComputedValue = data + iteration                             
   print( 'ComputedValue = %.1d for ID = (%.1d,%.1d)' %(ComputedValue,data,iteration) )

Currently it prints only for 5 cases: 目前仅打印5种情况:

ComputedValue = 4 for ID = (0,4)
ComputedValue = 5 for ID = (1,4)
ComputedValue = 6 for ID = (2,4)
ComputedValue = 7 for ID = (3,4)
ComputedValue = 8 for ID = (4,4)

What changes/modifications to be done, so it prints for all 25 cases? 要进行哪些更改/修改,以便针对所有25种情况进行打印? Thanks!! 谢谢!! Rachit Rachit

Edit: I don't want to print at the end of every for loop as I want to create a vector of computed values (25 values) and plot it with respect to some variable. 编辑:我不想在每个for循环的末尾打印,因为我想创建一个计算值(25个值)的向量并将其相对于某些变量进行绘制。

You need to put 你需要放

print( 'ComputedValue = %.1d for ID = (%.1d,%.1d)' %(ComputedValue,data,iteration) )

at the end of every inner for loop (but still inside of the loop, so not on the same line as the for) that it gets printed for every single iteration. 在每个内部for循环的末尾(但仍在循环内部,因此与for不在同一行),该值将在每次迭代中打印。

If you do the same stuff for every loop you could also consider to create a function which you call there with the print in it. 如果对每个循环都执行相同的操作,则还可以考虑创建一个函数,在其中调用并带有打印内容。

To receive a vector with all values in the end insert this line in the beginning (before the outer for loop): 要接收最后带有所有值的向量,请在开头(在外部for循环之前)插入以下行:

hist = []

Then in every inner for loop add this line: 然后在每个内部for循环中添加以下行:

hist.append(id)

Now you'll have all values available after you're done with the loops and can print them or do whatever you like with them. 现在,在完成循环后,您将拥有所有可用值,并可以打印它们或对它们进行任何操作。

I don't want to print at the end of every for loop as I want to create a vector of computed values (25 values) 我不想在每个for循环的末尾打印,因为我想创建一个计算值(25个值)的向量

Well, so why don't you just do that ("create a vector of computed values") then ??? 好吧,那为什么不这样做(“创建计算值的向量”)然后呢?

values = []
for i in range(5):
   for j in range(5):
       values.append((i, j))

print(values)

If you want to print after each iteration, without accumulating the result and printing it at the end (like in Bruno's answer) then you can use a simple function like in the following example: 如果您想在每次迭代后打印,而不累加结果并在最后打印(如Bruno的回答),则可以使用以下示例中的简单函数:

def f(data, r=range(5)):
    for iteration in r:
        ID = (data, iteration)
        print( 'ComputedValue = %.1d for ID = (%.1d,%.1d)' %(ComputedValue,data,iteration) )

for data in range(5):
    f(data)

You can modify the function as you think is best for your code, without the need of writing all that boilerplate code. 您可以按照自己认为最适合您的代码的方式修改函数,而无需编写所有样板代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM