简体   繁体   English

为什么我的python代码无限循环?

[英]Why is my python code looping infinitely?

Do you have any idea what i isn't incrementing? 你知道我没有增加什么吗? I'm trying to generate a weighted graph with n nodes? 我正在尝试生成具有n个节点的加权图?

i is my increment. 我是我的增量。

So if I call genWeightGraph(10), I want to add 10 nodes, adding node k to two vertexes (v1 and v2). 因此,如果我调用genWeightGraph(10),我想添加10个节点,将节点k添加到两个顶点(v1和v2)。 I am starting my graph with just two nodes connected to each other, so the list of their edges starts out as being [[1],[0]], with the index of list[index] = vertex. 我开始时只有两个互相连接的节点,因此它们的边列表以[[1],[0]]开始,索引为list [index] =顶点。 I'm generating k randomly from a set of N nodes, and I'm connecting K to 2 random vertexes. 我从一组N个节点中随机生成k个,并将K个连接到2个随机顶点。

The list weighted exists because, like the internet, the more connections/edges you have, the more likely new nodes are to connect to you. 之所以存在此加权列表,是因为像Internet一样,您拥有的连接/边缘越多,新节点连接到您的可能性就越大。 So weighted list just helps me account for this probability bias, as I pull/sample from this weighted list. 因此,当我从此加权列表中提取样本时,加权列表仅能帮助我解释这种概率偏差。

But it doesn't end. 但这并没有结束。 Ever. 曾经 I can't figure it out. 我不知道。

def genWeightGraph(n): #n nodes, davg number of links
    links = [[] for i in xrange(n-2)] # create n many nodes -2 to adjust for insert [1],[0]
    links.insert(0,[1])
    links.insert(1,[0]) # start with [[1],[0],...[]] of n length
    weighted = [nodes for v in links for nodes in v] #initialized weighted list of [1,0]
    i = 0 #initialized edges added
    while (i < n): #add this many nodes
        v1 = random.choice(weighted) #pick a friend/vertex from weighted list
        v2 = random.choice(weighted) #pick another friend/vertex from weighted list
        k = random.choice(xrange(2,n)) #pick a new friend to connect both v1 and v2 to
        print "v1", v1
        print "v2", v2 
        print "k", k
        print "nodes", i
        if k in links[v1] or links[v2]:
           continue
        elif v1 == v2: # if you pick the same vertex, just add k to one of them
            links[v1].append(k)
            links[k].append(v1)
            weighted += [k,v1] 
            i += 1
        else:
            links.insert(v1, k) # access v1's friend list, append k
            links.insert(k, v1) # find k's list, add v
            links.insert(v2, k) #add k to v2's list
            links.insert(k, v2) #find k's list, add v2 
            weighted += [k,v1,k,v2] #add to weighted
            i += 1

This is the problem: 这就是问题:

if k in links[v1] or links[v2]:

it is not how you use the or operator... it should be: 它不是您使用or运算符的方式...应该是:

if (k in links[v1]) or (k in links[v2]):

In your code, you always get true value once links[v2] is initialized. 在您的代码中,链接[v2]初始化后,您始终会获得真正的价值。

Consider a following example 考虑以下示例

>>> a = [1,2,3]
>>> b = [4,5,6]
>>> if 3 in a or b: print 'ok'
... 
ok
>>> if 13 in a or b: print 'bad'
... 
bad
>>> if 13 in a or 13 in b: print 'bad'
... 
>>> 

You have the following line 您有以下一行

if k in links[v1] or links[v2]:

which tests if k is in links[v1] or if links[v2] is not empty. 它测试kin links[v1]还是links[v2]不为空。 Change it to 更改为

if (k in links[v1]) or (k in links[v2]):

Because of that: 因此:

if k in links[v1] or links[v2]:
    continue

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

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