Python:在给定项目的2D列表中返回2个整数作为索引 [英] Python: Return 2 ints for index in 2D lists given item

查看:71
本文介绍了Python:在给定项目的2D列表中返回2个整数作为索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这周我一直在修改python,但我陷入了困境.
如果我有这样的2D列表:

I've been tinkering in python this week and I got stuck on something.
If I had a 2D list like this:

myList = [[1,2],[3,4],[5,6]]

我做到了

>>>myList.index([3,4])

它将返回

1

但是,我希望像这样在列表的第一侧进行索引

However, I want the index of something in side one of the lists, like this

    >>>myList.index(3)

它将返回

1, 0

有什么可以做到的吗?

Is there anything that can do this?

欢呼

推荐答案

尝试一下:

def index_2d(myList, v):
    for i, x in enumerate(myList):
        if v in x:
            return (i, x.index(v))

用法:

>>> index_2d(myList, 3)
(1, 0)

这篇关于Python:在给定项目的2D列表中返回2个整数作为索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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