0

I've created a list of objects each of which has an attribute which is a 5x5 numpy array with named columns and also an "ID" attribute. I want to create a function which checks if specific elements in the array (based on position) are in a (different) list of variable length, but exactly which array elements are being searched for can vary based on certain conditions.

Ideally I'd like to pass the list of subscripts used to retrieve the array elements as an argument to the function which will then attach each of the desired subscripts to the object and check for their presence in the list.

Here's what I mean:

# lst is the list we're checking against (i.e. are the array elements in this list)
# objs_list is the list of objects with the array and ID attributes
# "A" is the name of one of the columns in the numpy array

def check_list_membership(obj_id):
    # create object_to_check which is a numpy array
    object_to_check = next((x for x in objs_list if x.ID == obj_id)), None).arrayattribute

    if (object_to_check["A"][0] in lst) & (object_to_check["A"][1] in lst) & \
    (object_to_check["A"][2] in lst) & (object_to_check["A"][3] in lst ) & \
    (object_to_check["A"][4] in lst):
        print("all selected elements are in the list")
    else:
        print("one or more selected elements are not in the list")

In this example, I want to see if the array elements ["A"][0], ["A"][1], etc. are in the list. But in other cases I may want to check if ["A"][0], ["B"][1], and others are in the list. Obviously it's really clunky to have all of these conditional statements written out and I want to avoid this.

What I really want is to take the list of desired subscripts and attach each of them to the "object_to_check" in sequence and check for their membership in the list. I don't think string concatenation as described here will do what I want because I don't want the result to be strings. Instead I want each of these elements to be evaluated and checked for membership in the list. I don't think multiplying "object_to_check" to the length of the number of subscripts and zipping would help either because then I'd have to end up with strings (disembodied subscripts) and I'm not sure what I could do that would allow my list of object/subscript pairs to be evaluated (safely). I've looked into evaluating functions in string form which seems to be veering into controversial territory.

The desired function might look something like:

def check_list_membership(obj_id,list_of_subscripts):
    object_to_check = next((x for x in objs_list if x.ID == obj_id)), None).arrayattribute 

    # pass 'list_of_subscripts' in here and attach to 'object_to_check'
    if object_to_check[various subscripts] in lst:
        print("all selected elements are in the list")
    else:
        print("one or more selected elements are not in the list")

How can I accomplish this without dozens of lines of hard-coding?

7
  • you said "... I may want to check if ["A"][0], ["B"][1]" - that involves mapping between A and its subscripts and B and its subscripts. So object_to_check need to be checked for each string key separately Commented Feb 9, 2023 at 21:01
  • At a quick glance it looks like a pandas dataframe would handle these labeling issues. numpy does not have named rows or columns. Commented Feb 9, 2023 at 21:01
  • @hpaulj regarding named columns in numpy: stackoverflow.com/questions/7037938/numpy-named-columns Commented Feb 9, 2023 at 21:17
  • Yes, you can make a structured array, with fields instead of columns. That's convenient when loading mixed dtypes from a csv. But as a programming convenience when all all fields have the same dtype - I'm not sure it's worth it. You can readily select a field, but it's hard to do math, or comparisons, across fields. Commented Feb 9, 2023 at 22:51
  • Makes sense. It's not necessary for this to be in numpy - I'd be open to a pandas-based solution if there's a convenient one Commented Feb 10, 2023 at 1:30

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.