python列表中的所有值都是相同的

时间:2018-04-24 02:08:58

标签: python python-3.x

运行良好但游戏列表中每个游戏的所有时间字段都更新为最终值,例如所有4" Vic park"所有游戏都显示2145而不是4次。

class Game:
    time = 0
    team1 = ""
    team2 = ""

class Field:
    def __init__(self, numTimes = 0, name = "", daysAvail = 0):
    self.numTimes = numTimes
    self.games = [Game()] * numTimes
    self.name = name
    self.daysAvail = daysAvail

fieldList = []
fieldList.append(Field(4,"Vic Park", 5)) # 5 = monday and wednesday 0000101 base 2, lsb is monday, msb sunday
fieldList[0].games[0].time = 1800
fieldList[0].games[1].time = 1915
fieldList[0].games[2].time = 2030
fieldList[0].games[3].time = 2145
fieldList.append(Field(2,"Bannerman Park", 5))
fieldList[1].games[0].time = 1800
fieldList[1].games[1].time = 1915

0 个答案:

没有答案