简体   繁体   English

如何在python中随机通过double for循环

[英]How to go through a double for loop randomly in python

Consider the following code: 考虑以下代码:

for i in range(size-1):
    for j in range(i+1,size):
        print((i,j))

I need to go through this for-loop in a random fashion. 我需要以随机方式经历此for循环。 I attempt to write a generator to do such a thing 我试图写一个生成器来做这样的事情

def Neighborhood(size):
    for i in shuffle(range(size-1)):
        for j in shuffle(range(i+1), size):
            yield i, j
for i,j in Neighborhood(size):
    print((i,j))

However, shuffle cannot be applied to whatever object range is. 但是,随机播放不能应用于任何对象范围。 I do not know how to remedy the situation, and any help is much appreciated. 我不知道如何纠正这种情况,我们将不胜感激。 I would prefer a solution avoid converting range to a list, since I need speed. 我希望解决方案避免将范围转换为列表,因为我需要速度。 For example, size could be on the order of 30,000 and i will do perform this for loop around 30,000 times. 例如,大小可能在30,000量级,而我将执行此循环约30,000次。

I also plan to escape the for loop early, so I want to avoid solutions that incorporate shuffle(list(range(size))) 我还计划尽早逃避for循环,因此我想避免合并shuffle(list(range(size)))的解决方案

You can use random.sample . 您可以使用random.sample

The advantage of using random.sample over random.shuffle , is , it can work on iterators , so in : 使用的优点random.sample超过random.shuffle ,是,它可以在迭代器工作,所以在:

  • Python 3.X you don't need to convert range() to list Python 3.X,您无需将range()转换为list
  • In Python 2,X, you can use xrange 在Python 2,X中,您可以使用xrange
  • Same Code can work in Python 2.X and 3.X 相同的代码可以在Python 2.X和3.X中工作

Sample code : 样例代码:

n=10
l1=range(n)
for i in sample(l1,len(l1)):
    l2=range(i,n)
    for j in sample(l2,len(l2)):
        print(i,j)

Edit : 编辑:

As to why I put in this edit, go through the comments. 至于为什么要进行此编辑,请仔细阅读注释。

def Neighborhood(size):
    range1 = range(size-1)
    for i in sample(range1, len(range1)):
        range2 = range(i+1)
        for j in sample(range2, len(range2)):
            yield i, j

A simple way to go really random, not row-by-row: 一个真正随机的简单方法,而不是逐行:

def Neighborhood(size):
    yielded = set()
    while True:
        i = random.randrange(size)
        j = random.randrange(size)
        if i < j and (i, j) not in yielded:
            yield i, j
            yielded.add((i, j))

Demo: 演示:

for i, j in Neighborhood(30000):
    print(i, j)

Prints something like: 打印类似:

2045 5990
224 5588
1577 16076
11498 15640
15219 28006
8066 10142
7856 8248
17830 26616
...

Note: I assume you're indeed going to "escape the for loop early" . 注意:我假设您确实要“尽早退出for循环” Then this won't have problems with slowing down due to pairs being produced repeatedly. 这样就不会因为重复产生配对而减慢速度。

I don't think you can randomly traverse an Iterator. 我认为您不能随机遍历Iterator。 You can predefine the shuffled lists, though 不过,您可以预定义混排的列表

random iteration in Python Python中的随机迭代

L1 = list(range(size-1))
random.shuffle(L1) 
for i in L1:
    L2 = list(range(i+1, size))
    random.shuffle(L2) 
    for j in L2:
        print((i,j))

Of course, not optimal for large lists 当然,对于大型列表而言并不是最佳选择

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

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