使用pop(),list [-1]和+ =时,python中求值的顺序是什么? [英] What is the order of evaluation in python when using pop(), list[-1] and +=?

查看:119
本文介绍了使用pop(),list [-1]和+ =时,python中求值的顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

a = [1, 2, 3]
a[-1] += a.pop()

结果为[1, 6].

a = [1, 2, 3]
a[0] += a.pop()

这将导致[4, 2].这两个结果的评估顺序是什么?

This results in [4, 2]. What order of evaluation gives these two results?

推荐答案

首先是RHS,然后是LHS.而且在任何一侧,评估顺序都是从左到右.

RHS first and then LHS. And at any side, the evaluation order is left to right.

a[-1] += a.pop()a[-1] = a[-1] + a.pop()

a = [1,2,3]
a[-1] = a[-1] + a.pop() # a = [1, 6]

查看当我们更改RHS的操作顺序时行为如何变化,

See how the behavior changes when we change the order of the operations at RHS,

a = [1,2,3]
a[-1] = a.pop() + a[-1] # a = [1, 5]

这篇关于使用pop(),list [-1]和+ =时,python中求值的顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆