Python Forum
slice python array on condition
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
slice python array on condition
#1
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]: list
I would like to select the element corresponding to the value "A", but how? of course:
Error:
Array["A", 0] Traceback (most recent call last): File "<ipython-input-14-1b9f724cd495>", line 1, in <module> Array["A", 0] TypeError: list indices must be integers or slices, not tuple
`
Is there a way in pure Python, without Numpy or other apps?
Thank you


In [
Reply
#2
If you need do it many times, then using dict instead list:

A = dict(zip(Labels, Load))
print(A)
Output:
{'A': 0, 'B': 0, ......}
A['A']
Output:
0
Reply
#3
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']}")
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Fix pandas copy/slice warning. deanhystad 3 758 Sep-07-2023, 03:18 PM
Last Post: deanhystad
  Slice creates new objects? fmr300 4 1,268 Jul-20-2022, 12:34 PM
Last Post: fmr300
  InvalidIndexError: (slice(None, None, None), slice(None, -1, None)) SuperNinja3I3 1 4,288 Jul-15-2022, 05:59 AM
Last Post: Larz60+
  Python Pandas: How do I sumproduct by rows with an if condition? JaneTan 2 5,282 Jul-13-2021, 11:36 AM
Last Post: jefsummers
  Indexing [::-1] to Reverse ALL 2D Array Rows, ALL 3D, 4D Array Columns & Rows Python Jeremy7 8 6,963 Mar-02-2021, 01:54 AM
Last Post: Jeremy7
  Slice list Moris526 1 1,619 Dec-24-2020, 02:19 AM
Last Post: deanhystad
  increase and decrease a slice value? KEYS 2 2,055 Nov-10-2020, 11:35 PM
Last Post: KEYS
  Removing some elements from array based on a condition claw91 0 1,483 Oct-27-2020, 03:42 PM
Last Post: claw91
  else condition not called when if condition is false Sandz1286 10 5,739 Jun-05-2020, 05:01 PM
Last Post: ebolisa

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020