2016-12-04 80 views
0

所以我試圖找回的ñ商店列表,以使得它們的鄰居,然後,如果必要的話,鄰居的鄰居。以下是用於計算此列表的代碼,稱爲位置。商店的編號從1到10。的Python:list.pop(0)給出指數錯誤,而列表已滿

在這種情況下,每個商店有4個鄰居。這種關係在字典中被隨機設置,稱爲鄰居

import random 

shops = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

neighbours = {} 

for i in shops: 
    neighbours[i] = random.sample(shops, 4) 
    while i in neighbours[i]: 
     neighbours[i] = random.sample(shops, 4) 
print (neighbours) 
shop_zero = random.randrange(1,11) 
locations = [shop_zero] 
neighborhd = neighbours[locations[0]] 
n=10 
while len(locations) < n: 
    if not neighborhd: 
     print('if statement') 
     neighborhd = neighbours[random.choice(locations)] 
     print (neighborhd) 
    print('while loop') 
    ne = neighborhd.pop(0) 
    if ne not in locations: 
     locations.append(ne) 
print (locations) 

的問題是,代碼工作有時但常常給我一個索引錯誤:

IndexError: pop from empty list 

對於那些有興趣,以下是鄰居詞典輸出:

{1: [7, 5, 4, 9], 2: [5, 6, 3, 7], 3: [10, 8, 7, 6], 4: [7, 8, 10, 2], 5: [3, 6, 1, 9], 6: [5, 1, 10, 3], 7: [3, 8, 6, 2], 8: [10, 4, 9, 7], 9: [6, 5, 3, 2], 10: [3, 5, 8, 7]} 

我已經添加了一些打印語句以使工作示例更具信息性。正如我前面所說,它每隔一段時間都會有效,但大多數情況下會導致索引錯誤。 請幫忙?

P.S.我意識到,由此產生的列表並不總是給我的羣集,而是一個從鄰居到鄰居的路徑。這對我正在進行的項目來說很好。

+0

在'pop(0)'之前放一個'print(neighborhd)'並查看它的值。 –

+0

你似乎認爲行'neighborhd = neighbors [random.choice(locations)]'總是會給你一個非空的列表。我沒有看到任何理由爲什麼會這樣。你能澄清你的推​​理嗎? –

+0

@ A.Far請看下面的答案。謝謝 –

回答

0

我改變你的代碼調試的幫助,你可以看到下面。如果你運行這個,你會發現當拋出異常時,'鄰居'字典中的某些值有空列表。這意味着'鄰居'有可能被重新填充爲'鄰居'的空列表,所以仍然是空的。

import random 

shops = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

neighbours = {} 

for i in shops: 
    neighbours[i] = random.sample(shops, 4) 
    while i in neighbours[i]: 
     neighbours[i] = random.sample(shops, 4) 
print (neighbours) 
shop_zero = random.randrange(1,11) 
locations = [shop_zero] 
neighborhd = neighbours[locations[0]] 
n=10 
while len(locations) < n: 
    if not neighborhd: 
     print('if statement') 
     neighborhd = neighbours[random.choice(locations)] 
     print (neighborhd) 
    print('while loop') 
    try: 
     ne = neighborhd.pop(0) 
    except IndexError: 
     raise IndexError("Attempt to pop from neighborhd which has content %s, formed from neighbours which has content %s" % (neighborhd, neighbours)) 
    if ne not in locations: 
     locations.append(ne) 
print (locations) 
+0

啊感謝您瞭解發生了什麼!你有什麼想法,爲什麼我的字典將一些值設置爲空列表?我在print(neighbors)'行的while循環之前明確地檢查它們是否是空的(通過讀出它)。 –

+0

當您將「鄰居」列表中的某個列表分配給「鄰居」時,Python不會複製列表,但會對該列表進行新的引用。你從列表'鄰居'刪除成員指向,但這個列表仍然是'鄰居'值。這意味着當'鄰居'變爲空時,'鄰居'中的一個列表也是空的。 –

+0

閱讀關於python以及變量如何引用。也可以查看python副本,因爲你可能想在這裏使用它。 –

1

基本上,你在neighborhd = neighbours[random.choice(locations)]有問題。它也返回空列表。所以你需要做一點改變。 if neighborhd:pop之前只是檢查它不是一個空的列表。

import random 

shops = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

neighbours = {} 

for i in shops: 
    neighbours[i] = random.sample(shops, 4) 
    while i in neighbours[i]: 
     neighbours[i] = random.sample(shops, 4) 
print (neighbours) 
shop_zero = random.randrange(1,11) 
locations = [shop_zero] 
neighborhd = neighbours[locations[0]] 
n=10 
while len(locations) < n: 
    if not neighborhd: 
     print('if statement') 
     neighborhd = neighbours[random.choice(locations)] 
     print (neighborhd) 
    print('while loop') 
    if neighborhd: 
     ne = neighborhd.pop(0) 
     if ne not in locations: 
      locations.append(ne) 
print (locations) 
+0

當'neighborhd'和'neighbours'都爲空時,這會造成無限循環。嘗試把它放在一個for循環中,用於範圍(10000)中的j:'並運行它,在大約10之後它會遇到一個無限循環。 – rassar

+0

@rassar,我的錯誤代碼格式改變了代碼。現在試試。 –

+0

@RuhulAmin謝謝!這工作:-)雖然我的問題是爲什麼錯誤發生?我在while循環前打印(鄰居),就像我自己的理智檢查字典中沒有值是空列表一樣。它不是。但出於某種原因,有時候,某些值會變成空白列表。任何想法爲什麼? –

相關問題