简单的python循环不适用于许多内部循环

时间:2018-03-20 14:50:20

标签: python-3.x loops

我正在努力使用一个非常简单的循环,如果运行“独立”,它将完美地运行,但如果我将其用作许多其他指令的外循环(如果仅运行1次迭代,它也能完美地工作) )。

简单的外循环是

  

表示范围内的i(0,somevalue):   做一些内心的指示

这是完整的代码,如果我放置一个维度1的范围,它就完美无缺,而如果我放置一个简单的维度范围2,它就永远不会结束:

import numpy as np
import numpy.ma as ma
import random 
import matplotlib.pyplot as plt

i = int

x = np.zeros(1440)
class_x = np.zeros(1440)
w1 = np.array([0,6*60])
w2 = np.array([20*60,23*60])
x[w1[0]:(w1[1])] = np.full(np.diff(w1),0.001)
x[w2[0]:(w2[1])] = np.full(np.diff(w2),0.001)

x_masked = np.zeros_like(ma.masked_not_equal(x,0.001))

c = 10
func_time = 300
max_free_spot = int
i = 0

for i in range(0,1):
    tot_time = 0
    switch_count = 0
    switch_ons = []
    while
 tot_time <= func_time:
            switch_on = random.choice([random.randint(w1[0],(w1[1]-c)),random.randint(w2[0],(w2[1]-c))])
            if x[switch_on] == 0.001:
                if switch_on in range(w1[0],w1[1]):
                    if np.any(x[switch_on:w1[1]]!=0.001):
                        next_switch = [switch_on + k[0] for k in np.where(x[switch_on:]!=0.001)]
                        if (next_switch[0] - switch_on) >= c and max_free_spot >= c: 
                            upper_limit = min((next_switch[0]-switch_on),min(func_time,w1[1]-switch_on))
                        elif (next_switch[0] - switch_on) < c and max_free_spot >= c:
                            continue
                        else: upper_limit = next_switch[0]-switch_on

                    else:
                        upper_limit = min(func_time,w1[1]-switch_on) #max random length of cycle

                    if upper_limit >= c:
                        indexes = np.arange(switch_on,switch_on+(random.randint(c,upper_limit)))
                    else:
                        indexes = np.arange(switch_on,switch_on+upper_limit)
                else:
                    if np.any(x[switch_on:w2[1]]!=0.001):
                        next_switch = [switch_on + k[0] for k in np.where(x[switch_on:]!=0.001)]
                        if (next_switch[0] - switch_on) >= c:
                            upper_limit = min((next_switch[0]-switch_on),min(func_time,w2[1]-switch_on))
                        elif (next_switch[0] - switch_on) < c and max_free_spot >= c:
                            continue
                        else: upper_limit = next_switch[0]-switch_on
                    else:
                        upper_limit = min(func_time,w2[1]-switch_on)

                    if upper_limit >= c:
                        indexes = np.arange(switch_on,switch_on+(random.randint(c,upper_limit)))
                    else:
                        indexes = np.arange(switch_on,switch_on+upper_limit)


                tot_time = tot_time + indexes.size 
                switch_ons.append(switch_on)

                if tot_time > func_time:
                    indexes_adj = indexes[:-(tot_time-func_time)]
                    coincidence = random.randint(1,5)
                    np.put(x_masked,indexes_adj,(2*coincidence),mode='clip')
                    np.put(x,indexes_adj,(2*coincidence))
                    x_masked = np.zeros_like(ma.masked_greater_equal(x_masked,0.001))
                    tot_time = (tot_time - indexes.size) + indexes_adj.size 
                    switch_count = switch_count + 1
                    break
                else:
                    coincidence = random.randint(1,5)
                    np.put(x_masked,indexes,(2*coincidence),mode='clip')
                    np.put(x,indexes,(2*coincidence))
                    x_masked = np.zeros_like(ma.masked_greater_equal(x_masked,0.001))
                    tot_time = tot_time 
                    switch_count = switch_count + 1

                free_spots = [] 
                for j in ma.notmasked_contiguous(x_masked):               
                    free_spots.append(j.stop-j.start)
                max_free_spot = max(free_spots)
    class_x = class_x + x


plt.plot(class_x)

真的非常感谢任何帮助

0 个答案:

没有答案