Python:检查多个其他索引的索引

时间:2012-01-10 23:30:43

标签: python

我正在使用Python,我想检查列表列表中的值与其他索引的位置。

如果我在(1,1)处有1,我希望能够检查1是否在其他索引处,因此我可以根据匹配的索引来实现某些事情。

例如:

list_of_lists = [
    [4, 5, 6],
    [7, 1, 8],
    [6, 2, 9]
    ]
if 1 in row for row in list_of_lists:
    if index of 1 is (0, 0), (0, 1), or (0, 2)
        print ("It is in the First row!")
    if index of 1 is (1, 0), (1, 1), or (1, 2)
        print ("It is in the second row!")

如果这个工作正常,它应该打印:“它在第二行!”因为索引1与第三个if语句中的一个索引匹配。在我将使用它的某些情况下,它们可能不一定是行。所以,如果你能提供一种不会在答案中使用行的方法。只是一种查看索引并比较它们的方法。显然这不是正确的语法。但是我如何在Python中做到这一点?如何获得索引1并将其与列表列表中的其他索引进行比较?

谢谢!

4 个答案:

答案 0 :(得分:1)

这样的事情:

def lindex(list_of_lists, item):
    for l in list_of_lists:
        if item in l:
            return list_of_lists.index(l)

listoflists = [[4, 5, 6], [7, 1, 8], [6, 2, 9]]
item = 1
print "it is in the %. row" % lindex(listoflists, item)

至于您尝试快捷方式:

if 1 in row for row in list_of_lists:
  1. 语法不正确!
  2. 你无法避免查看列表中每一行的每一项 - 没有捷径
  3. 对于1,6的有效妥协,您可以尝试类似:

    rows = [i for listoflists in i in i]

  4. 这可能会给你一个空列表,意味着没有值为“1”的项目或列出包含“1”的行!

    然后,您可以打印包含1的所有行索引:

    for row in rows:
        print "it is in the %. row" % listoflists.index(row)
    

答案 1 :(得分:1)

在python文件中尝试:

list_of_lists = [
    [4, 5, 6],
    [7, 0, 8],
    [6, 2, 9]
]

def index_of(value, matrix):
    for i, r in enumerate(matrix):
        for j, c in enumerate(r):
            if c == value:
                # returns the first instance
                return (i,j)
    return None

if index_of(0, list_of_lists) == (1,1):
    print "hey"

list_of_lists2 = [
    [0, 5, 6],
    [7, 0, 8],
    [0, 2, 9]
]

def indexes_of(value, matrix):
    return [(i,j) for i, r in enumerate(matrix) for j, c in enumerate(r) if c == value]

print indexes_of(0, list_of_lists2)

输出

hey
[(0, 0), (1, 1), (2, 0)]

[编辑]根据要求:

首先,枚举是做什么的?

>>> seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]

source

如您所见,如果使用enumerate,它将返回元素的索引和元素。因此,当您迭代返回的列表时,您可以访问它们并按照您的意愿执行操作。这就是我在这里做的事情:

for i, r in enumerate(matrix)

在这种情况下,我代表行索引,r代表行本身。 OK?

在下一行你做同样的事情,但现在你要枚举行本身,好吗?现在,您将获得值j的索引和存储在每一行中的值c。

最后,当你返回(i,j)时,你只是返回一个代表值的矩阵索引的元组。

答案 2 :(得分:1)

这是一种方法:

>>> for lst in range(len(list_of_lists)):
...     if 1 in list_of_lists[lst]:
...             print "it's in list:", lst
... 
it's in list: 1
>>> 

请记住in运算符测试值是否出现在列表中(或列表中的对象)。

以下是使用index方法的第二种方式(在评论中讨论);

>>> for lst in range(len(list_of_lists)):
...     try:
...             _ = list_of_lists[lst].index(1)
...             print "1 is in list", lst
...     except ValueError:
...             continue
... 
1 is in list 1
>>> 

答案 3 :(得分:0)

for i,list in enumerate(list_of_lists, 1):
    if 1 in list:
        if i == 1:
            "it is in the first list"
        elif i == 2:
            "it is in the second list"
        ...

这是一个基本的实现。您可能希望对此进行参数化,但我不确定您的输入是什么。