|
楼主 |
发表于 2018-8-21 13:51:07
|
显示全部楼层
本帖最后由 东东枪 于 2018-8-21 13:52 编辑
创建这个外星人列表的方式不同导致了这个结果的不同,找到原因了,但是不清楚其原理,哪位大神可以解释一下:
这两种方式为何会呈现出不同的结果。
**********************A:建立了字典,alien_0存放一个绿色外星人,然后通过for循环 创建30个外星人*****************************
#创建了红色外星人和黄色外星人,颜色、得分、速度都不相同
alien_0 ={'color':'green','points':5,'speed':'slow'}
#然后通过for循环 创建30个外星人
aliens=[]
for num in range(0,30):
aliens.append(alien_0)#红色字体部分:使用append(),参数直接采用上文创建的alien_0 来实现创建了30个外星人
#然后执行修改前三个外星人的颜色、得分、速度
for alien1 in aliens[:3]:
alien1['color'] = 'yellow'
alien1['points'] = 10
alien1['speed'] = 'medium'
#结果发现全部修改了。
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
******************分割线**************************************
************************实现了理想的修改了前三个外星人的方法***********
*******B:不提前创建一个绿色外星人,在创建30个外星人的时候在循环里创建这个外星人然后append进去语句***************************************
aliens=[]
for num in range(0,30):
new_alien = {'color':'green','points':5,'speed':'slow'}#把新建绿色外星人的步骤放在这里非常关键,在下文的修改前三个外星人颜色时,列表里的每个外星人都是独立的对象,如果使用的方案A中的提前创建一个外星人,然后append()进去的话会导致一改全改。
aliens.append(new_alien )#
#然后执行修改前三个外星人的颜色、得分、速度
for alien1 in aliens[:3]:
alien1['color'] = 'yellow'
alien1['points'] = 10
alien1['speed'] = 'medium'
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'yellow', 'points': 10, 'speed': 'medium'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
{'color': 'green', 'points': 5, 'speed': 'slow'}
结论:
出现这两种不同结果的原因在于:
A方式 先创建了一个名为alien_0的字典包含一个外星人的信息,然后创建一个空列表,然后使用for循环30次,使用列表的append方法把这个字典添加了30次,然后修改前三个,却发现全部修改了
B方式 先创建一个空列表,然后使用for循环,在循环内创建一个字典new_alien 然后这个字典包含绿色外星人信息,然后使用append方法把这个new_alien加入列表aliens中,修改前三个,发现结果和预想的一样。
|
|