[英]Python: How do I assign the indexes of an item in a 2D list to two integer variables?
So I found out how to find the indexes of an item in a 2D list in Python by using this code: 因此,我发现了如何使用以下代码在Python的2D列表中查找项目的索引:
def index_2d(myList, v):
for i, x in enumerate(myList):
if v in x:
return (i, x.index(v))
Usage: 用法:
index_2d(myList, 3)
#Result (1, 0)
I want the output to be in two variables (like x = 1, y = 0). 我希望输出在两个变量中(例如x = 1,y = 0)。 How can I do this? 我怎样才能做到这一点?
In Python, you can return more than one value using tuples. 在Python中,您可以使用元组返回多个值。
def f():
return (1,2,3)
(x, y, z) = f()
In a more pythonic way to do it, you can only separate the values with ',' to indicate that it is a tuple. 用一种更加Python化的方式做到这一点,您只能用','分隔值以表明它是一个元组。
x, y, z = f()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.