I've been tinkering in python this week and I got stuck on something. If I had a 2D list like this: myList = [[1,2],[3,4],[5,6]]
and I did this
>>>myList.index([3,4])
it would return
1
However, I want the index of something in side one of the lists, like this
>>>myList.index(3)
and it would return
1, 0
Is there anything that can do this?
Cheers
Try this:
Usage:
This will return more than one instance of the sub list, if any.
There is nothing that does this already, unless it's in numpy, which I don't know much about. This means you'll have to write code that does it. And that means questions like "What does
[[1, 2], [2, 3], [3, 4]].index(3)
return?" are important.Based on kevpie's answer I managed to get a 2D list containing the coordinates of all occurences
Now coordsList contains all indexes for value 1 in myList :
Try this! this worked for me :)
If you are doing many lookups you could create a mapping.