找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 1681|回复: 0

[求助] 为什么结果不同?请求帮助

1

主题

1

帖子

1

积分

贫民

积分
1
yiwangdeyuan 发表于 2023-3-31 13:26:31 | 显示全部楼层 |阅读模式
alient = {'color':'green','point':5,'speed':'slow'} 在外面和在“for”中,为啥结果不同呀?

aliens = []
for alien_number in range(10):
        alient = {'color':'green','point':5,'speed':'slow'}
        aliens.append(alient)
print(aliens)
for alien in aliens[:3]:
        if alien['color'] == 'green':
                alien['color'] = 'yellow'
                alien['point'] = 10
                alien['speed'] = 'medium'
for alien in aliens[:5]:
        print(alien)
结果是:
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'green', 'point': 5, 'speed': 'slow'}
{'color': 'green', 'point': 5, 'speed': 'slow'}


aliens = []
alient = {'color':'green','point':5,'speed':'slow'}
for alien_number in range(10):
        aliens.append(alient)
print(aliens)
for alien in aliens[:3]:
        if alien['color'] == 'green':
                alien['color'] = 'yellow'
                alien['point'] = 10
                alien['speed'] = 'medium'
for alien in aliens[:5]:
        print(alien)

结果是:
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'yellow', 'point': 10, 'speed': 'medium'}
{'color': 'yellow', 'point': 10, 'speed': 'medium'}

回复

使用道具 举报

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

本版积分规则

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