[英]I want to use the result of a function in another function
some_list = [ [ ], [ ], [ ] ]
def my_func(sum):
...
a = 0
b = 1
return a, b
I'm trying to append to the given index in some_list
by getting the index from a
and b
.我正在尝试通过从
a
和b
获取索引来将some_list
到 some_list 中的给定索引。
What do I need to write so that this code below would work?我需要写什么才能使下面的代码起作用?
some_list[a].append(win)
some_list[b].append(loss)
Assign two variables at once to the call of your function, like this:一次将两个变量分配给 function 的调用,如下所示:
some_list = [ [ ], [ ], [ ] ]
def my_func(sum):
...
a = 0
b = 1
return a, b
a, b = my_func(sum) # this here
some_list[a].append(win)
some_list[b].append(loss)
Assuming by 'work' you mean that you want to use the return value of the function as the index of an array.假设“工作”意味着您想使用 function 的返回值作为数组的索引。
a, b = my_func(whatever)
some_list[a].append(win)
some_list[b].append(loss)
some_list = [ [ ], [ ], [ ] ]
def my_func(sum):
a = 0
b = 1
return a, b
sum = 5
a,b = my_func(sum)
win = 'aaa'
loss = 'bbb'
some_list[a].append(win)
some_list[b].append(loss)
print(some_list)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.