2017-04-21 60 views
0

這是我的代碼,我想知道你們中的任何人是否可以看看它,看看有什麼不對。迭代時,我一直在字典長度變化中發生的錯誤。我該如何解決?????基本上發生的事情是,我可以添加一些東西,並且他們工作得很好,但是當我真的把蒼蠅拿出來(因爲他們死了),它說它不會工作,因爲字典大小不斷變化。請幫助試圖克服我的字典和循環

import random 
from random import randint 

deathage = 30 
startingpop = 3 
rateofreproduction = 0 
howmanynewflies = 2 


def gennew(): 
    i = 0 
    list = [] 
    while i < 11: 
     i = i + 1 
     x = randint (1,4) 
     if x == 1: 
      list.append("A") 
     elif x == 2: 
      list.append("T") 
     elif x == 3: 
      list.append("C") 
     else: 
      list.append("G") 
    return list 

population = {} 

def createfly (x): 
    return ({"dna":x , "age":0}) 

for i in range(0,startingpop): 
    population[i] = (createfly(gennew())) 

print (population) 

def reproduce (x , y): 
    combos = [] 
    childfly = [] 
    for i in range (0,11): 
     combos.append((population[x]["dna"][i], population[y]["dna"][i])) 

    for i in range(0,len(combos)): 
     x = randint(0,1) 
     childfly.append(combos[i][x]) 
    return childfly 

#each gener**strong text**ation what happens 
while len(population) > 0: 
    for i in population: 
     population[i]["age"] = population[i]["age"] + 1 
     if population[i]["age"] > deathage: 
      population.pop(i) 
    flies = [] 
    for i in population: 
     flies.append(i) 
    for i in range(max(flies), (max(flies) + howmanynewflies)): 
     whichflyone = int(random.choice(list(population.keys()))) 
     whichflytwo = int(random.choice(list(population.keys()))) 
     population [i] = createfly(reproduce(whichflyone,whichflytwo)) 
    print() 
    print() 
    print(population) 

回答

1

我在下面標記了出現錯誤的位置。發生了什麼事情是您正在迭代​​並嘗試同時刪除元素。你不能這樣做,想象如果你迭代了一個10元素列表,並且在第一次迭代中移除了所有10個元素,python現在會嘗試迭代9個不存在的元素。

#each gener**strong text**ation what happens 
while len(population) > 0: 
    for i in population: 
     population[i]["age"] = population[i]["age"] + 1 
     if population[i]["age"] > deathage: 
      population.pop(i) # ERROR 
    flies = [] 
    for i in population: 
     flies.append(i) 
    for i in range(max(flies), (max(flies) + howmanynewflies)): 
     whichflyone = int(random.choice(list(population.keys()))) 
     whichflytwo = int(random.choice(list(population.keys()))) 
     population [i] = createfly(reproduce(whichflyone,whichflytwo)) 
    print() 
    print() 
    print(population) 

爲了解決這個問題,而不是刪除元素(與pop),你可以標記爲刪除元素(比如將它們添加到列表),除去循環之外:

#each gener**strong text**ation what happens 
while len(population) > 0: 
    removals = [] 
    for i in population: 
     population[i]["age"] = population[i]["age"] + 1 
     if population[i]["age"] > deathage: 
      removals.append(i) 
    for key in removals: 
     # population.pop(key) would also work. 
     del population[key] 
    flies = [] 
    for i in population: 
     flies.append(i) 
    for i in range(max(flies), (max(flies) + howmanynewflies)): 
     whichflyone = int(random.choice(list(population.keys()))) 
     whichflytwo = int(random.choice(list(population.keys()))) 
     population [i] = createfly(reproduce(whichflyone,whichflytwo)) 
    print() 
    print() 
    print(population) 

現在你的代碼運行了,儘管它似乎沒有停下來。如果這是你的預期行爲,那麼很好 - 但否則你可能會有更多的錯誤。 (例如,每個週期只能刪除一隻蒼蠅,因爲你的種羣有30只蒼蠅在0..30之間,並且你在下一個循環中總是添加一隻蒼蠅,所以你的while循環永遠不會中斷)。

+0

非常感謝你 –

+0

如果我已經回答了你的問題,請記住標記爲正確的問題 - 這樣可以避免其他用戶試圖回答已經回答的問題。 – Darkstarone

1

提示:您​​可以簡單地寫成

def gennew(): 
    return random.sample('ATCG', 10) 

此外,做這樣的事情:

for i in range(0,startingpop): 
    population[i] = (createfly(gennew())) 

幾乎是*毫無意義的(除非您存儲您的一批 「名」),你可以做

population = [createfly(gennew()) for i in range(startingpop)] 

你會得到一個列表更容易比imo更容易在這種情況下是字典。