2012-01-10 82 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" 
     ... 

這是一個基本的實現。你會想參數化這個,但我不確定你的輸入是什麼。