Python 追加到列表,用最后一个值替换所有以前的索引
Python append to list replacing all previous indexes with last value
在下面的Python3代码中,正确的值被写入daysSchedule
但是当迭代到下一个值时。
class Data:
def getDaysSchedule(scheduleUrl, filterTeam = ''):
with urllib.request.urlopen(scheduleUrl) as request:
data = json.loads(request.read().decode())
daysSchedule = GameList()
for gameData in data["dates"][0]["games"]:
game = Game()
game.gameId = gameData["gamePk"]
game.home.name = gameData["teams"]["home"]["team"]["name"]
game.away.name = gameData["teams"]["away"]["team"]["name"]
print('\n' + str(game.gameId)) # CORRECT VALUE HERE
daysSchedule.games = daysSchedule.games[:] + [game] # HERE HAVE TRIED APPEND BUT THAT DIDN'T WORK EITHER
# DEBUG CODE
menuItems = daysSchedule.games
for testItem in menuItems:
testId = testItem.gameId
title = testItem.home.name + ' VS. ' + testItem.away.name
print(title)
return daysSchedule
我从上面的调试代码块中得到如下输出。每一个都是从其中一行中写出的。您会看到一个新行被添加了正确的值,但是 所有之前的行也被错误地覆盖了这个值。
633855
Chicago Cubs VS. San Diego Padres
Chicago Cubs VS. San Diego Padres
633834
Arizona Diamondbacks VS. New York Mets
Arizona Diamondbacks VS. New York Mets
Arizona Diamondbacks VS. New York Mets
633853
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
633882
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
这到底是怎么回事 -- 除了我今天显然没有摄入足够的咖啡因。
这里是游戏 class
class Game(object):
gameId = ''
link = ''
date = ''
time = ''
status = Status()
home = Team()
away = Team()
series = Series()
venue = Venue()
... 和游戏列表 class:
class GameList(object):
games = [Game()]
所有的麻烦都来自于您使用 classes 的方式。请注意区别:
这个:
class Game(object):
gameId = ''
link = ''
date = ''
time = ''
status = Status()
home = Team()
away = Team()
series = Series()
venue = Venue()
比
class Game():
def __init__(self):
self.gameId = ''
self.link = ''
self.date = ''
self.time = ''
self.status = Status()
self.home = Team()
self.away = Team()
self.series = Series()
self.venue = Venue()
在前者中,您有 class attributes 属性 对于此 class 的所有实例。而后面的是实例属性,即针对特定实例.
的属性
知道了,我们现在看到外循环如何在每次迭代中将 Game
class 的新实例添加到 GameList
的实例,同时是不同的对象(与不同的 id()
,内存位置),它们 都共享它们的 class 属性 ,即当程序访问 game.home.name
时,所有实例都是相同的Game
class.
它就像一个在所有 class 实例之间共享的全局变量,尽管创建了(在任何其他意义上都是独立的)实例 class.
,但在每次迭代时都会对其进行修改
顺便说一句,我们可以同时拥有:
class Foo:
class_attr = 'I am shared among all the instances'
def __init__(self):
self.instance_attr = 'I belong only to a particular instance'
在此处的第二种情况下,特定实例将作为 self
隐式传递。
您已将 id 内置函数用作变量。更改变量名
在下面的Python3代码中,正确的值被写入daysSchedule
但是当迭代到下一个值时。
class Data:
def getDaysSchedule(scheduleUrl, filterTeam = ''):
with urllib.request.urlopen(scheduleUrl) as request:
data = json.loads(request.read().decode())
daysSchedule = GameList()
for gameData in data["dates"][0]["games"]:
game = Game()
game.gameId = gameData["gamePk"]
game.home.name = gameData["teams"]["home"]["team"]["name"]
game.away.name = gameData["teams"]["away"]["team"]["name"]
print('\n' + str(game.gameId)) # CORRECT VALUE HERE
daysSchedule.games = daysSchedule.games[:] + [game] # HERE HAVE TRIED APPEND BUT THAT DIDN'T WORK EITHER
# DEBUG CODE
menuItems = daysSchedule.games
for testItem in menuItems:
testId = testItem.gameId
title = testItem.home.name + ' VS. ' + testItem.away.name
print(title)
return daysSchedule
我从上面的调试代码块中得到如下输出。每一个都是从其中一行中写出的。您会看到一个新行被添加了正确的值,但是 所有之前的行也被错误地覆盖了这个值。
633855
Chicago Cubs VS. San Diego Padres
Chicago Cubs VS. San Diego Padres
633834
Arizona Diamondbacks VS. New York Mets
Arizona Diamondbacks VS. New York Mets
Arizona Diamondbacks VS. New York Mets
633853
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
Baltimore Orioles VS. Minnesota Twins
633882
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
New York Yankees VS. Tampa Bay Rays
这到底是怎么回事 -- 除了我今天显然没有摄入足够的咖啡因。
这里是游戏 class
class Game(object):
gameId = ''
link = ''
date = ''
time = ''
status = Status()
home = Team()
away = Team()
series = Series()
venue = Venue()
... 和游戏列表 class:
class GameList(object):
games = [Game()]
所有的麻烦都来自于您使用 classes 的方式。请注意区别:
这个:
class Game(object):
gameId = ''
link = ''
date = ''
time = ''
status = Status()
home = Team()
away = Team()
series = Series()
venue = Venue()
比
class Game():
def __init__(self):
self.gameId = ''
self.link = ''
self.date = ''
self.time = ''
self.status = Status()
self.home = Team()
self.away = Team()
self.series = Series()
self.venue = Venue()
在前者中,您有 class attributes 属性 对于此 class 的所有实例。而后面的是实例属性,即针对特定实例.
的属性知道了,我们现在看到外循环如何在每次迭代中将 Game
class 的新实例添加到 GameList
的实例,同时是不同的对象(与不同的 id()
,内存位置),它们 都共享它们的 class 属性 ,即当程序访问 game.home.name
时,所有实例都是相同的Game
class.
它就像一个在所有 class 实例之间共享的全局变量,尽管创建了(在任何其他意义上都是独立的)实例 class.
,但在每次迭代时都会对其进行修改顺便说一句,我们可以同时拥有:
class Foo:
class_attr = 'I am shared among all the instances'
def __init__(self):
self.instance_attr = 'I belong only to a particular instance'
在此处的第二种情况下,特定实例将作为 self
隐式传递。
您已将 id 内置函数用作变量。更改变量名