2012-01-10 80 views
0

我正在使用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的索引并将它与列表中的其他索引进行比较?

谢谢!

+0

我认为这个问题会更清晰,如果你用功能来解释它的话。例如,你是否想要一个可以让你这样做的函数:'list_index(list_of_lists,1)'并且得到(1,1)?这看起来像你想要做的,但你一直提到0,我不知道在这种情况下意味着什么 – 2012-01-10 23:48:44

+0

可能重复[检查索引与索引列表](http://stackoverflow.com/ question/8783148/check-indexes-against-lists-with-indexes) – wim 2012-01-11 00:31:19

+0

@wim很抱歉,我似乎没有收到任何东西,我决定重申我的问题,并从不同的角度出发。它似乎有效! – cbbcbail 2012-01-11 02:41:52

回答

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

正如你所看到的,如果你用枚举,它会返回元素和元素的两个指标。因此,当您迭代返回的列表时,您可以访问它们并执行您的要求。这就是我在这里做什么:

for i, r in enumerate(matrix) 

在这种情况下,i表示行索引,并且r为行本身。好?

在接下来的一行中,你做同样的事情,但现在你正在枚举行本身,好吗?现在,您将获得存储在每行中的值j和值c的索引。最后,当你返回(i,j)时,你只是返回一个表示值矩阵索引的元组。

+0

谢谢,我认为这真的会有帮助! – cbbcbail 2012-01-10 23:59:59

+0

你能告诉我它是如何工作的吗?谢谢! – cbbcbail 2012-01-11 00:06:14

+0

你能更具体吗?哪部分困扰你? – wleao 2012-01-11 02:43:09

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. 对于halway有效的妥协1.你可以尝试这样的:

    行= [我为我在listoflists如果1 I]

这将在任一给你一个空列表,这意味着没有项目值为“1”或列表包含“1”行!

然后,您可以打印包含1与所有行指数:

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

有些情况下,我检查的不同索引不在行中。我怎么会这样做,它不会看着行? – cbbcbail 2012-01-10 23:45:20

+1

YOu无法检查索引是否连续检查...您是否有一些先前的信息可​​以让您事先知道某个值不在“list_of_lists”中? – 2012-01-10 23:46:34

+0

在我的“代码”的开始处,我有“如果1行in list_of_lists:”这应该检查它是否在list_of_lists。 – cbbcbail 2012-01-10 23:49:55

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 
>>> 
+0

如果我不总是想检查它们是否在行中,我怎么能使用它? – cbbcbail 2012-01-10 23:51:24

+0

你的意思是,不使用'in'?有几种方法,你可以使用'index'方法:'mylist.index(3)'返回列表mylist中值为'3'的索引。 – snim2 2012-01-10 23:53:51

+0

是的,我知道该怎么做。但是,我怎么能够检查索引(3)是否是我的问题中的三个索引之一? – cbbcbail 2012-01-10 23:57:34

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" 
     ... 

这是一个基本的实现。你会想参数化这个,但我不确定你的输入是什么。