list.index() help - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: list.index() help (/thread-28862.html) |
list.index() help - qmfoam - Aug-06-2020 I have a 2D list called sampleArray. type(sampleArray) shows that it is a list. A small sample of the list is: sampleArray = [['2020-07-22', 10256, 296, 57, 10481, 49, 389377, -10, -10, 15, 25650, 7007], ['2020-07-23', 12504, 292, 36, 10987, 51, 401881, 21, 20, 30, 25942, 7043], ['2020-07-24', 12213, 307, 41, 11294, 53, 414094, -2, -21, 45, 26249, 7084], ['2020-07-25', 9393, 186, 18, 10819, 50, 423487, -11, -24, 73, 26435, 7102], ['2020-07-26', 8918, 204, 14, 10344, 48, 432405, -5, -4, 45, 26639, 7116], ['2020-07-27', 9276, 208, 23, 10077, 47, 441681, -5, -8, 40, 26847, 7139], ['2020-07-28', 9482, 226, 26, 9928, 46, 451163, -8, -32, 0, 27073, 7165], ['2020-07-29', 10015, 211, 14, 9950, 46, 461178, -20, -12, 6, 27284, 7179], ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195], ['2020-07-31', 9679, 97, 10, 9713, 45, 479905, 3, -23, -3, 27515, 7205], ['2020-08-01', 7227, 9, 1, 9092, 42, 487132, -19, -31, 15, 27524, 7206]]I want to find out the index associated with particular date entry. So I naively tried sampleArray.index('2020-07-25')and receive in response: ValueError: '2020-07-25' is not in the list. But it is - its located at sampleArray[3][0]. Ok, so I tried sampleArray.index(9679)and receive the same response: ValueError: 9679 is not in the list. Yet it is located at sampleArray[9][1] So I took just one row from sampleArray: test = sampleArray[8] testand have returned: ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195]I try test.index('2020-07-30') test.index(134)and get the returns I am expecting: 0 and 2. So there is something about a 2D list and .index() that I haven't figured out yet. Can anyone offer some insight as to what I am doing wrong? TNX RE: list.index() help - buran - Aug-06-2020 sampleArray is 2D - i.e. list of lists. list.index() method does not work on multiple levels deep. from the docs:Quote: list.index(x[, start[, end]]) spam = [[1, 2], [3, 4]] eggs = [3, 4] print(spam.index(eggs))
RE: list.index() help - bowlofred - Aug-06-2020 list.index() is for finding an element in a 1D list. It won't descend into sub-lists that represent multi-dimensional arrays. If you want to roll your own, you'll have to try to look for the data in each row separately. Something like: sampleArray = [['2020-07-22', 10256, 296, 57, 10481, 49, 389377, -10, -10, 15, 25650, 7007], ['2020-07-23', 12504, 292, 36, 10987, 51, 401881, 21, 20, 30, 25942, 7043], ['2020-07-24', 12213, 307, 41, 11294, 53, 414094, -2, -21, 45, 26249, 7084], ['2020-07-25', 9393, 186, 18, 10819, 50, 423487, -11, -24, 73, 26435, 7102], ['2020-07-26', 8918, 204, 14, 10344, 48, 432405, -5, -4, 45, 26639, 7116], ['2020-07-27', 9276, 208, 23, 10077, 47, 441681, -5, -8, 40, 26847, 7139], ['2020-07-28', 9482, 226, 26, 9928, 46, 451163, -8, -32, 0, 27073, 7165], ['2020-07-29', 10015, 211, 14, 9950, 46, 461178, -20, -12, 6, 27284, 7179], ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195], ['2020-07-31', 9679, 97, 10, 9713, 45, 479905, 3, -23, -3, 27515, 7205], ['2020-08-01', 7227, 9, 1, 9092, 42, 487132, -19, -31, 15, 27524, 7206]] find_it = '2020-07-25' for row_index, row in enumerate(sampleArray): if find_it in row: print(f"[{row_index}][{row.index(find_it)}]") Otherwise, you could convert this to a numpy array and use numpy index.import numpy as np sampleArray = [['2020-07-22', 10256, 296, 57, 10481, 49, 389377, -10, -10, 15, 25650, 7007], ['2020-07-23', 12504, 292, 36, 10987, 51, 401881, 21, 20, 30, 25942, 7043], ['2020-07-24', 12213, 307, 41, 11294, 53, 414094, -2, -21, 45, 26249, 7084], ['2020-07-25', 9393, 186, 18, 10819, 50, 423487, -11, -24, 73, 26435, 7102], ['2020-07-26', 8918, 204, 14, 10344, 48, 432405, -5, -4, 45, 26639, 7116], ['2020-07-27', 9276, 208, 23, 10077, 47, 441681, -5, -8, 40, 26847, 7139], ['2020-07-28', 9482, 226, 26, 9928, 46, 451163, -8, -32, 0, 27073, 7165], ['2020-07-29', 10015, 211, 14, 9950, 46, 461178, -20, -12, 6, 27284, 7179], ['2020-07-30', 9048, 134, 16, 9724, 45, 470226, -26, -12, -21, 27418, 7195], ['2020-07-31', 9679, 97, 10, 9713, 45, 479905, 3, -23, -3, 27515, 7205], ['2020-08-01', 7227, 9, 1, 9092, 42, 487132, -19, -31, 15, 27524, 7206]] find_it = '2020-07-25' arr = np.array(sampleArray) print(np.where(arr == find_it))
RE: list.index() help - deanhystad - Aug-06-2020 Index only works on 1D lists because that is all there is in Python, You can make something that looks like a 2D list but it is really just a list of lists. That is why our index() call failed. Your list "sampleArray" only contains lists. None of these lists is '2020-07-25' or 9679, so index() correctly raises a ValueError. bowlofred's numpy example works because numpy understands lists that contain lists that contain lists and knows how to do a deep search. RE: list.index() help - qmfoam - Aug-07-2020 buren, bowlofred, deanhystad Thanks for the info. I like the idea of using numpy so will go with that. |