![]() |
slice python array on condition - 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: slice python array on condition (/thread-22195.html) |
slice python array on condition - Gigux - Nov-03-2019 Hello, I have a python 2D array that looks like this: Labels = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", \ "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", \ "AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL", \ "MM", "NN"] Load = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] Array = [Labels, Load] print(Array) [['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'BB', 'CC', 'DD', 'EE', 'FF', 'GG', 'HH', 'II', 'JJ', 'KK', 'LL', 'MM', 'NN'], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] type(Array) Out[11]: listI would like to select the element corresponding to the value "A", but how? of course:
`Is there a way in pure Python, without Numpy or other apps? Thank you In [ RE: slice python array on condition - AlekseyPython - Nov-03-2019 If you need do it many times, then using dict instead list: A = dict(zip(Labels, Load)) print(A)
A['A']
RE: slice python array on condition - Larz60+ - Nov-03-2019 AlekseyPython's code is correct, however might be a bit confusing with the dictionary named the same as one of it's elements, Easier to understand if: zipped_dict = dict(zip(Labels, Load)) print(f"zipped_dict element 'A' = {zipped_dict['A']}") |