gpt4 book ai didi

Python:在 while 循环中返回多个值的优雅方式

转载 作者:行者123 更新时间:2023-11-28 16:29:14 29 4
gpt4 key购买 nike

我几乎只是在做这个......

while not something():
pass

但我希望函数 something() 返回除 True 或 False 之外的另一个值,特别是一个列表。在 C 中,我只是通过插入一个引用作为参数来返回它,但 Python 似乎不喜欢那样做。因此,在 something() 函数中,我显然只会返回 2 个值。我没意见。我遇到的问题是带有 while 循环的代码看起来不够优雅,因此我认为一定有更好的方法。

在 C 中:

int a[2];
while(!something(a)); //presume that 'a' updates and the loop breaks

在 Python 中:

bool_value, a = something()
while not bool_value:
bool_value, a = something()

我知道这没什么大不了的,但我必须将同一行写两次这一事实似乎有点糟糕。是否可以使用某种语法来调用函数并在 while 行内全部返回“a”?

最佳答案

while True:
bool_value, a = something()
if not bool_value:
break

关于Python:在 while 循环中返回多个值的优雅方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728536/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com