Can I assign the value of the left hand side of a Boolean expression (e.g in a while loop) to something?

别等时光非礼了梦想. 提交于 2019-12-12 03:05:32

问题


Is there a syntax to capture a result evaluated in a Boolean expression? In other words, what was the result that caused the Boolean to be True?

A concrete example - I want the second largest number in a set, regardless of how often the largest number is duplicated:

nums = [1, 2, 3, 4, 4]
h = nums.pop()
while nums.pop() == h:
    pass
print(nums.pop())

Obviously returns 2, because the 3 has gone in evaluating the expression. Is there something like:

while (i = nums.pop()) == h:

#Do something with i

来源:https://stackoverflow.com/questions/39855895/can-i-assign-the-value-of-the-left-hand-side-of-a-boolean-expression-e-g-in-a-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!