Reputation: 1771
If I have a list of lists in Python, and I make lists from parts of that list of lists, how could I check which list an index were in.
For example:
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]]
(These indices are completely intentional.) The 1 in this list of lists (LOL) is at index LOL[1][1]
(1,1). How would I check which out of the three lists: list1, list2, and/or list3, it is in?
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.)
If there was actual Python Syntax inside the parenthesis that would accomplish the described task, list1 would be searched and because there aren't any 6's in that list the if statement would fall through. How would I fill in the part in parenthesis so that it had the correct Python syntax to accomplish the task described there?
Upvotes: 0
Views: 246
Reputation: 29611
The list1, 2 and 3 do not contain references to the numbers in the LOL. Consider this:
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]
>>>
So the code you show, once executed, causes the following to be in Python interpreter:
LOL = [
[4, 5, 6],
[7, 1, 8],
[6, 2, 9]
]
list1 = [4, 5, 1]
list2 = [9, 2, 8]
list3 = [6, 8, 6]
Ie, there are no references to numbers, just the numbers themselves.
Since 1 is unique you could find by element search which list1/2/3 it is in, but for a number like 8, there would be two answers (list2 and list3). Also, there is no way of knowing, as shown in my first code sample, which LOL item the number corresponds to: is list3[0] the LOL[0][2] or the LOL[2][0]? There is no way of knowing.
OTOH, if you store objects in your LOL lists instead of numbers, you can find them by reference. Consider this code:
>>> a,b,c=[0],[1],[2]
>>> ll=[a,b,c]
>>> ll
[[0], [1], [2]]
>>> a[0]=5
>>> ll
[[5], [1], [2]]
>>>
This shows that ll is storing references to a, b, c, because those are themselves objects.
Then it is possible to find which of a,b,c contains a given number, and to find where the number is specifically in 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)
Given the above, I suspect the answer is that you cannot get what you are looking for, unless you rewrite your LOL and 3 lists to contain objects, like this:
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]
Then if you are looking for c, which has same number as e but is a different object, you would do
index = getIndex(c[0], list1)
if index >= 0:
return getLOLItem(list1[index])
where
def getLOLItem(item):
for subLOL in LOL:
if item in subLOL:
return subLOL.index(item)
Upvotes: 1
Reputation: 29611
There are two many issues with the question, and too many comments, so I'm putting the following in an answer:
Firstly, in
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]]
Are the indices correct? Or should they be
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]]
Secondly,
The one in this list of lists is at index LOL[1][1].
This statement does not make sense. By 'one' you are referring to '1', which is at 'index' (1,1) in the sense that LOL[1][1] contains '1'. LOL[1][1] is not an index, it is a value (in fact, it refers to a value).
How would I check which list it is in?
Which list are you talking about? The list1, list2 or list3 (ie, find in which of those 3 the '1' is referenced), or LOL[0], LOL[1], LOL[2] (ie find in which 'row' is '1' referenced)? Because LOL contains 3 items, each one a list (of integers). LOL does not contain any integers (the items in LOL contain integers).
For this reason,
if 1 in LOL:
is not valid Python: LOL does not contain any integers. It does make sense if it is pseudocode.
Another issue:
(search the list that the 1 is in for 0.
0 is not in any list, this is confusing, is it a typo?
LOL is just an example there could be multiple ones.)
Multiple LOL? How is the fact that there are multiple LOL relevant to the task of finding something in LOL? Or perhaps what you really have is this:
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]]
where I picked the LOL0/1/2/3 randomly as an example. In this case what you are trying to solve is, if you find that '1' is in LOL2, at what (row, col) index is it in LOL2? The row col will satsify LOL2[row][col] is '1'. But this doesn't make much sense to me either.
Finally, if you could provide an example answer like "and the answer would be list2" or "and the answer would be LOL[1]", that can help a lot with understanding what you are trying to achieve.
Please clarify your post with all of the above to minimize how much back and forth commenting occurs, there is a huge amount of noise right now and I'm really tempted to flag the question for closure so you can start from scratch (I feel it is a good question so I hope to see it through).
Upvotes: 1
Reputation: 1310
It seems there is confusion in the use of "index" here. I'll take a stab at it. This will return the name of the sublist and the index within the sublist. I think others have shown how to find the index in the original 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)
Upvotes: 0
Reputation: 27585
Maybe this is what you want:
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)
return
1
2
Maybe.
By the way, what list1,list2,list3 are playing here ?
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)
result
[(1, 1)]
[(0, 2), (2, 0)]
Upvotes: 0
Reputation: 4448
val = 1
for i, lst in enumerate(LOL):
if val in lst:
print(i, lst.index(val))
will print "1, 1".
Upvotes: 0
Reputation: 799430
if 1 in some_list:
...
As for how to check the different variables, don't. Put them in a list or dict instead.
Upvotes: 0