3

Say I have a 2-dimensional array of strings like so:

A = [['a', 'b', 'b'],
     ['c', 'c', 'a'],
     ['d', 'c', 'a']]

and I want to find out how many rows a given element appears in, so that I get the output:

In [1]: get_number_rows('a')
Out[1]: 3

In [2]: get_number_rows('b')
Out[2]: 1

In [1]: get_number_rows('c')
Out[1]: 2

In [2]: get_number_rows('d')
Out[2]: 1

Note that I don't want the total number of occurrences of 'a', but the number of rows it appears in.

I have tried looping over the rows and simply counting, but I'm dealing with a very large dataset (1000s x 1000s), so it's very slow. Any faster solutions would be appreciated.

3 Answers 3

1

For rows, try something like

len([x for x in A if 'a' in x])

This list comprehension makes a list of all lists x in A satisfying the condition that 'a' in x. You then take the length of that list to get the total number of them.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for this, I still struggle a bit with list comprehension!
1

You can use the following get_number_rows() method to sum if the number of arrays that that contain the character:

A = [['a', 'b', 'b'],
     ['c', 'c', 'a'],
     ['d', 'c', 'a']]

def get_number_rows(char):
    return len([x for x in A if char in x])

get_number_rows('a')
>> 3

get_number_rows('b')
>> 1

get_number_rows('c')
>> 2

get_number_rows('d')
>> 1

Comments

0
if __name__ == "__main__":
    A = [['a', 'b', 'b'],
         ['c', 'c', 'a'],
         ['d', 'c', 'a']]

    def get_number_rows(A, desired_element):
        """
        Pass in two dimensional array, A as first parameter
        Pass in desired char element, desired_element as 2nd parameter.
        Function will return number of occurrences of desired_element in A.
        """

        element_count = 0  # Int to keep track of occurrences
        for group in A:  # For nested array in A
            if desired_element in group:  # If our desired element is in the sub array
                element_count += 1  # Increment our counter
        return element_count  # After completion, return the counter

    print(get_number_rows(A, 'a'))

    print(get_number_rows(A, 'b'))

    print(get_number_rows(A, 'c'))

    print(get_number_rows(A, 'd'))

Comments

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.