Python:返回给定项目的二维列表中索引的 2 个整数

发布于 2024-11-02 21:53:02 字数 394 浏览 2 评论 0原文

这周我一直在修改 python,但我遇到了一些问题。
如果我有一个像这样的二维列表:

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

并且我这样做了,

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

它会返回

1

但是,我想要其中一个列表中某个内容的索引,就像这样

    >>>myList.index(3)

,它会返回

1, 0

有什么可以做到这一点吗?

干杯

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]]

and I did this

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

it would return

1

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

    >>>myList.index(3)

and it would return

1, 0

Is there anything that can do this?

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

吃兔兔 2024-11-09 21:53:02

试试这个:

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)

Try this:

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)
(1, 0)
虐人心 2024-11-09 21:53:02

如果您进行多次查找,您可以创建一个映射。

>>> myList = [[1,2],[3,4],[5,6]]
>>> d = dict( (j,(x, y)) for x, i in enumerate(myList) for y, j in enumerate(i) )
>>> d
{1: (0, 0), 2: (0, 1), 3: (1, 0), 4: (1, 1), 5: (2, 0), 6: (2, 1)}
>>> d[3]
(1, 0)

If you are doing many lookups you could create a mapping.

>>> myList = [[1,2],[3,4],[5,6]]
>>> d = dict( (j,(x, y)) for x, i in enumerate(myList) for y, j in enumerate(i) )
>>> d
{1: (0, 0), 2: (0, 1), 3: (1, 0), 4: (1, 1), 5: (2, 0), 6: (2, 1)}
>>> d[3]
(1, 0)
贵在坚持 2024-11-09 21:53:02

使用简单的geneexpr:

def index2d(list2d, value):
    return next((i, j) for i, lst in enumerate(list2d) 
                for j, x in enumerate(lst) if x == value)

示例

print index2d([[1,2],[3,4],[5,6]], 3)
# -> (1, 0)

Using simple genexpr:

def index2d(list2d, value):
    return next((i, j) for i, lst in enumerate(list2d) 
                for j, x in enumerate(lst) if x == value)

Example

print index2d([[1,2],[3,4],[5,6]], 3)
# -> (1, 0)
五里雾 2024-11-09 21:53:02

目前还没有任何东西可以做到这一点,除非是在 numpy 中,而我对此不太了解。这意味着您必须编写代码来执行此操作。这意味着诸如“[[1, 2], [2, 3], [3, 4]].index(3) 返回什么?”之类的问题。很重要。

There is nothing that does this already, unless it's in numpy, which I don't know much about. This means you'll have to write code that does it. And that means questions like "What does [[1, 2], [2, 3], [3, 4]].index(3) return?" are important.

愁杀 2024-11-09 21:53:02
def td(l,tgt):
    rtr=[]
    for sub in l:
        if tgt in sub:
            rtr.append(    (l.index(sub),sub.index(tgt))    )

    return rtr        


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

print td(myList,3)

这将返回子列表的多个实例(如果有)。

def td(l,tgt):
    rtr=[]
    for sub in l:
        if tgt in sub:
            rtr.append(    (l.index(sub),sub.index(tgt))    )

    return rtr        


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

print td(myList,3)

This will return more than one instance of the sub list, if any.

肩上的翅膀 2024-11-09 21:53:02

试试这个!这对我有用:)

def ret_pos(mylist,val_to_find):
    for i in (len(mylist)):
        for j in (len(i)):
            if mylist[i][j]== val_to_find:
                postn=[i,j]
    return(postn);

Try this! this worked for me :)

def ret_pos(mylist,val_to_find):
    for i in (len(mylist)):
        for j in (len(i)):
            if mylist[i][j]== val_to_find:
                postn=[i,j]
    return(postn);
惜醉颜 2024-11-09 21:53:02

根据kevpie的答案我设法获得了一个包含所有出现的坐标的2D列表

myList = [[0,1],[1,1],[0,0],[1,0]]
coordsList = [[x, y] for x, li in enumerate(myList) for y, val in enumerate(li) if val==1]

现在coordsList包含值1的所有索引我的列表:

[[0, 1], [1, 0], [1, 1], [3, 0]]

Based on kevpie's answer I managed to get a 2D list containing the coordinates of all occurences

myList = [[0,1],[1,1],[0,0],[1,0]]
coordsList = [[x, y] for x, li in enumerate(myList) for y, val in enumerate(li) if val==1]

Now coordsList contains all indexes for value 1 in myList :

[[0, 1], [1, 0], [1, 1], [3, 0]]
浸婚纱 2024-11-09 21:53:02

警告:
第一个答案是:

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

对于每个 x 中的任何 v,只要存在任何相等/相同的元素,就会导致很多错误。因为 x.index(v) 只返回 x 中具有相同 v 值的任何元素的第一个(最左边)索引,而不是当前 v 的实际索引。也就是说,仅当每行中的所有元素都有效时,这才有效是独一无二的。

相反,尝试这样做以避免非唯一值:

import numpy
arr_2D = numpy.array(...)
                  
for x, row in enumerate(arr_2D.tolist()):
    for y, cell in enumerate(row):
    cell_pos = (x, y)

Caution:
The first answer up there:

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

will cause a lot of errors as long as there are any equal/identical elements, for any v in each x. Because x.index(v) only returns the first (leftmost) index of any element that has the same value of v, in x, NOT the actual index of the current v. That is, this only works if all elements in each row are unique.

Instead, try this to avoid non-unique values:

import numpy
arr_2D = numpy.array(...)
                  
for x, row in enumerate(arr_2D.tolist()):
    for y, cell in enumerate(row):
    cell_pos = (x, y)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文