在列表列表中找到项目的索引 [英] Find the index of an item in a list of lists

查看:117
本文介绍了在列表列表中找到项目的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表列表:

colours = [["#660000","#863030","#ba4a4a","#de7e7e","#ffaaaa"],["#a34b00","#d46200","#ff7a04","#ff9b42","#fec28d"],["#dfd248","#fff224","#eefd5d","#f5ff92","#f9ffbf"],["#006600","#308630","#4aba4a","#7ede7e","#aaffaa"]]

搜索列表并返回其中一项的位置的最干净方法是什么,例如"#660000"?

我已经看过index方法,但这似乎并没有将列表解压缩到列表中.

postion = colours.index("#660000")

给出:ValueError: ['#660000'] is not in list,而不是我所期望的[0][0] ...

解决方案

我会做这样的事情:

[(i, colour.index(c))
 for i, colour in enumerate(colours)
 if c in colour]

这将返回一个元组列表,其中第一个索引是第一个列表中的位置,第二个索引是第二个列表中的位置(注意:c是您要查找的颜色,即).

对于问题中的示例,返回的值为:

[(0, 0)]

如果只需要以一种懒惰的方式找到找到颜色的第一个位置,则可以使用以下方法:

next(((i, colour.index(c))
      for i, colour in enumerate(colours)
      if c in colour),
     None)

这将返回找到的第一个元素的元组,如果找不到元素,则返回None(您也可以删除上面的None参数,如果没有找到元素,它将引发StopIteration异常). /p>

正如@RikPoggi正确指出的那样,如果匹配数很高,这将带来一些开销,因为对colour进行了两次迭代以查找c.我认为这对于少量匹配并在单个表达式中给出答案是合理的.但是,为避免这种情况,您还可以使用与以下相同的思想来定义方法:

def find(c):
    for i, colour in enumerate(colours):
        try:
            j = colour.index(c)
        except ValueError:
            continue
        yield i, j

matches = [match for match in find('#660000')]

请注意,由于find是生成器,因此您实际上可以像上面的示例那样使用next来使用它,以在第一个比赛时停止并跳过进一步的搜索.

I have a list of lists:

colours = [["#660000","#863030","#ba4a4a","#de7e7e","#ffaaaa"],["#a34b00","#d46200","#ff7a04","#ff9b42","#fec28d"],["#dfd248","#fff224","#eefd5d","#f5ff92","#f9ffbf"],["#006600","#308630","#4aba4a","#7ede7e","#aaffaa"]]

What's the cleanest way of searching the list and returning the position of one of the items, e.g. "#660000"?

I have looked at the index method, but that doesn't seem to unpack the list inside the list.

postion = colours.index("#660000")

gives: ValueError: ['#660000'] is not in list, not [0][0] as I expect...

解决方案

I'd do something like this:

[(i, colour.index(c))
 for i, colour in enumerate(colours)
 if c in colour]

This will return a list of tuples where the first index is the position in the first list and second index the position in the second list (note: c is the colour you're looking for, that is, "#660000").

For the example in the question, the returned value is:

[(0, 0)]

If you just need to find the first position in which the colour is found in a lazy way you can use this:

next(((i, colour.index(c))
      for i, colour in enumerate(colours)
      if c in colour),
     None)

This will return the tuple for the first element found or None if no element is found (you can also remove the None argument above in it will raise a StopIteration exception if no element is found).

Edit: As @RikPoggi correctly points out, if the number of matches is high, this will introduce some overhead because colour is iterated twice to find c. I assumed this to be reasonable for a low number of matches and to have an answer into a single expression. However, to avoid this, you can also define a method using the same idea as follows:

def find(c):
    for i, colour in enumerate(colours):
        try:
            j = colour.index(c)
        except ValueError:
            continue
        yield i, j

matches = [match for match in find('#660000')]

Note that since find is a generator you can actually use it as in the example above with next to stop at the first match and skip looking further.

这篇关于在列表列表中找到项目的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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