使用Python使用随机范围遍历列表

时间:2018-12-04 09:29:39

标签: python python-3.x

我对Python还是很陌生,所以如果这个问题很简单,请原谅,但是我尝试搜索并且还没有找到实现这种方法的方法。

我正在使用随机步骤遍历两个数字的范围,并打印出有关它们的一些信息。到目前为止,这是我的代码:

more +X [file_containing data] > [file_to_export_data_to]

我希望能够打印出step变量,并使其在循环的每次迭代中都(随机)更改,但是代码仅给我一个值,该值在每次迭代中都会重复一次。这是示例输出:

from random import randint

#generate random number between 200 and 400
step = randint(200,400)
num = 0

for x in range(440000,450000,step):
    num = num + 1
    print("{}: {}km, increased by {}km".format(num,x,step))

我该如何真正使循环真正随机化?任何帮助都将不胜感激。

谢谢!

3 个答案:

答案 0 :(得分:3)

您无法使用for循环执行此操作,因为range采取了 initial 步骤(由于C / C ++ for循环,因此常常会造成混淆可以修改循环中的步骤或当前索引的旧版本。

我会使用经典的while循环:

from random import randint

#generate random number between 200 and 400
num = 0

x = 440000

while True:
    num += 1
    step = randint(200,400)
    x += step
    if x >= 450000:
        break
    print("{}: {}km, increased by {}km".format(num,x,step))

结果:

1: 440380km, increased by 380km
2: 440586km, increased by 206km
3: 440975km, increased by 389km
4: 441221km, increased by 246km
5: 441545km, increased by 324km
6: 441803km, increased by 258km
7: 442080km, increased by 277km
8: 442476km, increased by 396km
9: 442822km, increased by 346km
10: 443179km, increased by 357km
11: 443536km, increased by 357km
12: 443779km, increased by 243km
13: 444170km, increased by 391km
14: 444449km, increased by 279km
15: 444791km, increased by 342km
16: 445023km, increased by 232km
...
28: 449154km, increased by 348km
29: 449431km, increased by 277km
30: 449647km, increased by 216km
31: 449890km, increased by 243km

我不确定是否进行极限检查,因此我避免执行while x < 450000,而是使用了无限循环,以便以后可以添加并检查(否则,我觉得我将无法以“模拟” for循环。

答案 1 :(得分:0)

这是因为您要在计算之前定义步骤,并且仅计算一次。 您的代码在每次执行循环时都会为您提供274的固定步长值

在这种情况下,for循环并不理想(尽管有人可以提供解决方案)。 在这种情况下,您可以使用while循环来获得所需的结果

start = 440000 # this will be the starting km
num = 1        # Counter

while start <= 450000: # End value is 450000 km
    step = randint(200,400)       # This calls a new random int for each loop
    print ("{}: {}km, increased by {}km".format(num,start,step))
    start = start + step          # add start and step to give new start km
    num = num + 1                 # increment counter

答案 2 :(得分:0)

在Python中,当您想在for循环中做一些复杂的事情时,通常值得将复杂的部分提取到生成器中。

from random import randint

def randstep(start, end, stepmin, stepmax):
    current = start
    yield current, 0
    while current < end:
        step = randint(stepmin, stepmax)
        current += step
        if current < end:
            yield current, step

for num, (x, step) in enumerate(randstep(44000, 45000, 200, 400)):
    print("{}: {}km, increased by {}km".format(num,x,step))

$ python3 t.py
0: 44000km, increased by 0km
1: 44338km, increased by 338km
2: 44587km, increased by 249km
3: 44954km, increased by 367km
$ python3 t.py
0: 44000km, increased by 0km
1: 44374km, increased by 374km
2: 44598km, increased by 224km
3: 44815km, increased by 217km

因此,对于实际的循环,您需要使用while,但是您可以将循环提取到生成器randstep中,然后可以在for循环中使用它。您还希望有一个计数器num,并且已经有了一个生成器enumerate,因此无需执行num = num + 1