简体   繁体   English

在 for 循环 python 中更改索引值

[英]Change value of index in a for loop python

AN implementation of Crohn(Крона) algorithm used in Scheduling theory, is it possible to change the data of the current index in a for loop in python?调度理论中使用的 Crohn(Крона) 算法的实现,是否可以在 python 的 for 循环中更改当前索引的数据? I have a code like so;我有这样的代码; link to the full code链接到完整代码

#list1 is a 2d array
list1 = [[12, 3, 17], [14], [10, 12, 15]]


cond = 1
while cond:
    d = delta(sum_calc(list1))
#delta() finds the difference between the list with the highest sum
#and the list with the minimum sum, then returns the
#difference(d[0]), index_of_list_with_max_sum(d[1]), #index_of_list_with_min_sum(d[2])
#d = [23, 2, 1]

    if cond == 0:
        break
    else:
        for i in list1[d[1]]:
            if d[0] > i:
                move(list1[d[1]], list1[d[2]])
#move() moves the min element from list1, to list2
            else:
                cond = 0

what I am trying to do is, given an index, loop through the elements of that list, then check if num is greater than i (element), after that we do some operations, like moving the smallest element from the current list we are looping from, to another list.我想做的是,给定一个索引,循环遍历该列表的元素,然后检查 num 是否大于 i(元素),然后我们执行一些操作,例如从当前列表中移动最小的元素从,循环到另一个列表。 then we have some operations, after that I want to change the value of i, to something like然后我们进行一些操作,之后我想将 i 的值更改为类似

#the index might change
i = l[index]

the problem I am facing is that when I do that, it continues looping from the first index.我面临的问题是,当我这样做时,它会继续从第一个索引循环。 Is there a way I can reset it, so that it starts looping from other elements?有没有办法可以重置它,以便它开始从其他元素循环?

I HAVE USED A WHILE LOOP, because I want the procedure to repeat itself and until d[0]:> i:我使用了一个 WHILE LOOP,因为我希望该过程不断重复,直到 d[0]:> i:

#since the list with the highest sum is list1[2], we got 2 from d,
#next step is to loop through the elements in that list and check if 
#there's an element which is less than d[0](the difference between max and min sum)


#expected results
#after first iteration
list1 = [[12, 3, 17], [14, 10], [12, 15]]
d = [8, 0, 1]

#after second iteration
list1 = [[12, 17], [14, 10, 3], [12, 15]]
d = [2, 0, 1]

#the problem is it stops here, I want it to calculate delta again then #repeat the process, but after debugging, i found that in the second #iteration i = 15, which the second element of the list, but it should #be like that.

Try to combine while loop with index?尝试将while循环与索引结合起来?

example:例子:

lst = [1,2,3]

idx = 0
while idx < len(lst):
   print(lst[idx])
   idx += 1
   if idx == len(lst):
      # Reset index
      idx = 0

EDIT编辑

After debugging I found your errors - You havent been assigning the new delta result to d where you have been referencing from your code thus you never got the new indexes调试后我发现了你的错误 - 你没有将新的delta结果分配给你从代码中引用的d ,因此你从来没有得到新的索引

cond = True
idx = 0
while cond and idx < len(l[d[1]]):
    if d[0] > l[d[1]][idx]:
        move(l[d[1]], l[d[2]])
        s = sum_calc(l)
        d = delta(s)
        print("l =", l)
        print("s =", s)
        print("d =", d)
        print("")
        idx = 0
    else:
        cond = False
    idx += 1

Output: Output:

l = [[12, 3, 17], [14, 10], [12, 15]]
s = [32, 24, 27]
d = [8, 0, 1]


l = [[12, 17], [14, 10, 3], [12, 15]] 
s = [29, 27, 27]
d = [2, 0, 1]

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

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