我无法将单个元素附加到嵌套列表

时间:2018-06-06 17:57:28

标签: python list nested

我正在尝试将一个元素添加到嵌套列表中的列表中。但是,元素会被添加到嵌套列表中的每个列表中,而不仅仅是它应该进入的列表。我尝试使用相同的代码,它在我的程序之外完美地工作,所以我将在这里添加我的整个程序。我为我的一般不良形式和低效率道歉,但我仍然相对较新的编码。谢谢!

import pygame
import sys
from random import randint
import math
pygame.init()
black = 0,0,0
white = 255,255,255
red = 255,0,0
blue = 0,0,255
screen = pygame.display.set_mode((500,500))
screen.fill(white)

citiesX = []
citiesY = []
visits = []
paths = []
lengths = []
cities = []
list1 = []

#Sets the colour of the line depending on its length
def getColour(CurrentLength):
    percentage = (CurrentLength/1400)*100
    colour = 2.55*percentage
    return(colour)

#Renders Lines
def drawLines(visits):
    global citiesX
    global citiesY
    screen.fill(white)
    pygame.draw.circle(screen, red, (citiesX[0],citiesY[0]), 5, 4)
    #Draw Cities
    for i in range (1,len(citiesX)):
        pygame.draw.circle(screen, black, (citiesX[i],citiesY[i]), 5, 4)
    #Draw Paths
    for i in range (0,len(visits)-1):
        #Colour Rendering
        lengthX = (citiesX[visits[i]]-citiesX[visits[i+1]])*(citiesX[visits[i]]-citiesX[visits[i+1]])
        lengthY = (citiesY[visits[i]]-citiesY[visits[i+1]])*(citiesY[visits[i]]-citiesY[visits[i+1]])
        CurrentLength = math.sqrt(lengthX+lengthY)
        #Draw Lines
        pygame.draw.lines(screen, (getColour(CurrentLength),getColour(CurrentLength),getColour(CurrentLength)), False, [(citiesX[visits[i]],citiesY[visits[i]]),(citiesX[visits[i+1]],citiesY[visits[i+1]])], 4)
        pygame.display.update() 



#Find out how long a generated path is
def getPathLength(visits):
    totalLength = 0
    for i in range (0,len(visits)-1):
        lengthX = (citiesX[visits[i]]-citiesX[visits[i+1]])*(citiesX[visits[i]]-citiesX[visits[i+1]])
        lengthY = (citiesY[visits[i]]-citiesY[visits[i+1]])*(citiesY[visits[i]]-citiesY[visits[i+1]])
        CurrentLength = math.sqrt(lengthX+lengthY)
        pygame.draw.lines(screen, (getColour(CurrentLength),getColour(CurrentLength),getColour(CurrentLength)), False, [(citiesX[visits[i]],citiesY[visits[i]]),(citiesX[visits[i+1]],citiesY[visits[i+1]])], 4)
        pygame.display.update()
        totalLength +=CurrentLength
    return(totalLength)

#Evolve the paths
def evolve(gen):
    global paths
    global lengths
    global citiesX
    global visits
    global cities
    global list1
    for i in range (0,100):
        visits = []
        visits.append(0)
        #for i in range (1,count):
        #   visits.append(gen[i])
        x = 0
        while len(visits)<len(citiesX):
            num = list1[x][randint(0,len(citiesX)-1)]
            while (num in visits):
                num = list1[x][randint(0,len(citiesX)-1)]
            visits.append(num)
            x = x+1

        paths.append(visits)
        lengths.append(getPathLength(visits))

    genPicker = sorted(lengths)
    gen = paths[lengths.index(genPicker[0])]
    bad = paths[lengths.index(genPicker[99])]
    print(gen)
    drawLines(gen)
    gen.remove(0)
    bad.remove(0)
#THIS IS WHERE THE ISSUE IS!!!!!!!
    for i in gen:
       list1[gen.index(i)].append(i)
    print(list1)
    paths = []
    lengths = []
    #count = count+1
    evolve(gen)

#Randomly Generates the cities
for i in range (0,10):
    citiesX.append(randint(0,500))
    citiesY.append(randint(0,500))

#lists used for evolution
for x in range(0,len(citiesX)):
    cities.append(x)
print("Cities: ",cities)
for i in range(0,len(cities)-1):
    list1.append(cities)
print(list1)

#Draws the cities
pygame.draw.circle(screen, red, (citiesX[0],citiesY[0]), 5, 4)
for i in range (1,len(citiesX)):
    pygame.draw.circle(screen, black, (citiesX[i],citiesY[i]), 5, 4)

#Creates First Generation
for i in range (0,100):
    visits.clear()
    visits.append(0)

    while len(visits)<len(citiesX):
        num = cities[randint(0,len(citiesX)-1)]
        while num in visits:
            num = cities[randint(0,len(citiesX)-1)]
        visits.append(num)

    paths.append(visits)
    lengths.append(getPathLength(visits))

#Gets the best path from the first generation
genPicker = sorted(lengths)
#Stores Gen1
gen1 = paths[lengths.index(genPicker[0])]
print(gen1)
#Draws Gens1
drawLines(gen1)
#resets varaibles
paths = []
lengths = []
count = 1

#The program evolves the path
evolve(gen1)

1 个答案:

答案 0 :(得分:2)

#THIS IS WHERE THE ISSUE IS!!!!!!!
for i in gen:
   list1[gen.index(i)].append(i)

问题是你如何创建list1。列表一只包含20次相同的对象。

创建时:

for i in range(0,len(cities)-1):
    list1.append(cities)

您只是将相同的列表放入list1。另一方面,这会附加一个克隆:

for i in range(0,len(cities)-1):
    list1.append(cities[:]) # note the :.