繁体   English   中英

检查具有索引的列表的索引

[英]Check indexes against lists with indexes

如果我在Python中有一个列表列表,并且我从列表列表的部分列表,我怎么能检查索引所在的列表。

例如:

LOL = [
    [4, 5, 6],
    [7, 1, 8],
    [6, 2, 9]
    ]
list1 = [LOL[0][0], LOL[0][1], LOL[1][1]]
list2 = [LOL[2][2], LOL[2][1], LOL[1][2]]
list3 = [LOL[2][0], LOL[1][2], LOL[0][2]]

(这些指数完全是有意的。)此列表清单(LOL)中的LOL[1][1]位于索引LOL[1][1] (1,1)。 我如何检查三个列表中的哪一个:list1,list2和/或list3,它在哪?

if 1 in row for row in LOL:
    (search the list that the 1 is in for 6. The integers contained in the lists in LOL are just examples, there could be multiple 1's in the lists in LOL.)

如果在括号内有实际的Python语法可以完成所描述的任务,那么将搜索list1,因为该列表中没有任何6,if语句将会通过。 我如何填写括号中的部分,以便它具有正确的Python语法来完成那里描述的任务?

问题有两个问题,评论太多,所以我将以下内容放在一个答案中:

首先,在

 list1 = [LOL[0][0], LOL[0][1], LOL[1][1]] list2 = [LOL[2][2], LOL[2][1], LOL[1][2]] list3 = [LOL[2][0], LOL[1][2], LOL[0][2]] 

指数是否正确? 或者他们应该

  list1 = [LOL[0][0], LOL[0][1], LOL[0][2]] 
  list2 = [LOL[1][0], LOL[1][1], LOL[1][2]] 
  list3 = [LOL[2][0], LOL[2][1], LOL[2][2]]

其次,

此列表列表中的列表位于索引LOL [1] [1]。

这句话没有意义。 '1'表示'1',即LOL [1] [1]包含'1'的意义上的'索引'(1,1)。 LOL [1] [1]不是索引,它是一个值(实际上,它指的是一个值)。

我该如何查看它所在的列表?

你在谈论哪个列表? list1,list2或list3(即,在3中引用'1'中的哪一个),或者LOL [0],LOL [1],LOL [2](即找到'row'为'1')参考)? 因为LOL包含3个项目,每个项目都是一个列表(整数)。 LOL不包含任何整数(LOL中的项包含整数)。

为此原因,

如果在LOL中1:

是无效的Python:LOL不包含任何整数。 如果它是伪代码,它确实有意义。

另一个问题:

(搜索1表示0的列表。

0不在任何列表中,这是令人困惑的,这是一个错字吗?

LOL只是一个例子,可能有多个。)

多个LOL? 有多个LOL与在LOL中找到某些东西的任务相关的事实是怎样的? 或者你真正拥有的是这个:

  list1 = [LOL2[1][2], LOL1[0][2], LOL3[2][2]]
  list2 = [LOL2[1][2], LOL2[0][2], LOL0[2][2]]
  list3 = [LOL1[1][2], LOL0[0][2], LOL3[0][2]]

我随机选择了LOL0 / 1/2/3作为例子。 在这种情况下,你要解决的是,如果你发现LOL2中的'1',那么LOL2中的(row,col)索引是什么? 行col将饱和LOL2 [row] [col]为'1'。 但这对我来说也没什么意义。

最后,如果您可以提供一个示例答案,例如“并且答案将是list2”或“并且答案将是LOL [1]”,这可以帮助您理解您想要实现的目标。

请用上述所有内容澄清你的帖子,以尽量减少来回评论的发生,现在有大量的噪音,我真的很想提出关闭的问题,所以你可以从头开始(我觉得它是一个很好的问题,所以我希望看到它。

list1,2和3不包含对LOL中数字的引用。 考虑一下:

Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ll=[1,2,3]
>>> kk=[ll[1], ll[0], ll[2]]
>>> kk
[2, 1, 3]
>>> ll[2]=5
>>> kk
[2, 1, 3]
>>>

因此,您显示的代码一旦执行,就会导致以下内容出现在Python解释器中:

LOL = [
    [4, 5, 6],
    [7, 1, 8],
    [6, 2, 9]
    ]
list1 = [4, 5, 1]
list2 = [9, 2, 8]
list3 = [6, 8, 6]

即,没有数字的引用,只有数字本身。

由于1是唯一的,你可以通过元素搜索找到它所在的list1 / 2/3,但对于像8这样的数字,会有两个答案(list2和list3)。 另外,如我的第一个代码示例所示,无法知道数字对应的LOL项目是:list3 [0] LOL [0] [2]还是LOL [2] [0]? 没有办法知道。

OTOH,如果您将对象存储在LOL列表而不是数字中,您可以通过引用找到它们。 考虑以下代码:

>>> a,b,c=[0],[1],[2]
>>> ll=[a,b,c]
>>> ll
[[0], [1], [2]]
>>> a[0]=5
>>> ll
[[5], [1], [2]]
>>>

这表明ll存储对a,b,c的引用,因为它们本身就是对象。

然后可以找到a,b,c中包含给定数字的哪一个,并找到数字在ll中的具体位置:

if 2 in a: 
   return ll.index(a)
if 2 in b: 
   return ll.index(b)
if 2 in c: 
   return ll.index(c)

鉴于上述情况,我怀疑答案是你无法得到你想要的东西,除非你重写你的LOL和3个列表来包含对象,如下所示:

a,b,c,d,e,f,...=[0],[1],[2],[4],[2],[6],...
LOL=[[a,b,c],[d,e,f],[g,h,i]]
list1=[a,c,f]
list2=[d,a,b]
list3=[f,g,a]

然后,如果你正在寻找与e具有相同数量但是是不同对象的c,你会这样做

index = getIndex(c[0], list1)
if index >= 0:
    return getLOLItem(list1[index])

哪里

def getLOLItem(item):
    for subLOL in LOL: 
       if item in subLOL:
          return subLOL.index(item)
if 1 in some_list:
   ...

至于如何检查不同的变量,请不要。 把它们放在一个列表或字典中。

val = 1
for i, lst in enumerate(LOL):
    if val in lst:
        print(i, lst.index(val))

将打印“1,1”。

这里使用“索引”似乎有些混乱。 我会捅它。 这将返回子列表的名称和子列表中的索引。 我认为其他人已经展示了如何在原始LOL中找到索引。

LOL = [
    [4, 5, 6],
    [7, 1, 8],
    [6, 2, 9],
    ] 

#store the sublists in a dictionary
checkLists = {
    'list1':[LOL[0][0], LOL[0][1], LOL[1][1]],
    'list2':[LOL[2][2], LOL[2][1], LOL[1][2]],
    'list3':[LOL[2][0], LOL[1][2], LOL[0][2]] 
    }

#the value you are looking for
checkVal = 1

#recursive function to search for a value in a list of lists
def containsValue(val,items):
    for item in items:
        if isinstance(item,(list,tuple)):
            if find(val,item):
                return True
        else:
            print item
            if item == val:
                return True
    return False

#Now check to see if and where the checkVal exists in the sublists
if containsValue(checkVal, LOL):
    #search each sublist for the checkVal
    for k,v in checkLists.items():
        #see if the value is in the sublist
        if checkVal in v:
            #return the sublist key and index where we found the value
            i = v.index(checkVal)
            return (k,i)

也许这就是你想要的:

LOL = [[4, 5, 6],
       [7, 1, 8],
       [6, 2, 9] ]

list1 = [LOL[0][0], LOL[0][1], LOL[1][1]]
list2 = [LOL[2][2], LOL[2][1], LOL[1][2]]
list3 = [LOL[2][0], LOL[1][2], LOL[0][2]]


def sublist_index_of_li_in_which_is_x(li,x):
    return (i for i,sublist in enumerate(li) if x in sublist).next()


print sublist_index_of_li_in_which_is_x(LOL,1)
print sublist_index_of_li_in_which_is_x(LOL,9)

返回

1
2

也许。

顺便说一下,list1,list2,list3在这里玩什么?

编辑

def rowcol_tuples_locating_x_in_li(li,x):
    return [(i,sublist.index(x)) for i,sublist in enumerate(li) if x in sublist]


print rowcol_tuples_locating_x_in_li(LOL,1)
print rowcol_tuples_locating_x_in_li(LOL,6)

结果

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM