鱼C论坛

 找回密码
 立即注册
查看: 2114|回复: 1

列表内嵌套字典,通过for循环修改字典的值,设想修改三个,实际却修改了全部???

[复制链接]
发表于 2018-8-21 10:32:21 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
问题描述:
#创建一个列表,然后创建几个外星人的字典,包含颜色、得分、速度
#修改前三个外星人的颜色、得分、速度
#期望实现仅修改了前三个,但是实际却修改了全部的



#创建了红色外星人和黄色外星人,颜色、得分、速度都不相同
alien_0 ={'color':'green','points':5,'speed':'slow'}
alien_1 ={'color':'yellow','points':10,'speed':'medium'}

#建立一个空的列表,用以存储外星人们
aliens = []

#通过for循环 创建30个外星人
for num in range(0,30):
        aliens.append(alien_0)

#需要改前三个外星人的颜色、得分、速度
for alien1 in aliens[:3]:
    alien1['color'] = 'yellow'
    alien1['points'] = 10
    alien1['speed'] = 'medium'

#打印全部的外星人
for alien in aliens[:]:
    print(alien)

#返回结果如下
>>>
=== RESTART: C:/Users/WangDong/Desktop/复习/python/class1 入门学习/6.4.1嵌套练习.py ===
{'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'}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 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中,修改前三个,发现结果和预想的一样。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-26 08:57

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表