maxischl
maxischl

Reputation: 631

Count index combinations in two lists of same length

I have two lists: list_a and list_b of the same length. However, the length is variable, the following example is just a simplification.

The values in the arrays are variable, too, but I know which values could possibly occur. The optional values are listed in options_a and options_b.

list_a = ['metro', 'metro', 'urban', 'urban', 'rural']
list_b = [1, 2, 2, 2, 3]

options_a = [1, 2, 3, 4]
options_b = ['metro', 'urban', 'suburb', 'rural']

I want to count the occurrences of option_b-options for each option in options_a and print that in a matrix:

[
 [1, 0, 0, 0],       #tells me that options_a=1 has the option_b='metro' exactly once
 [1, 2, 0, 0],       #options_a=2 has the option_b='metro'once, option_b='urban' twice
 [0, 0, 0, 1],       #options_a=3 has the option_b='rural' exactly once
 [0, 0, 0, 0]        #options_a=4 did not occur
]

However, my approach does not work, and I don't know how to correct it:

output = list(map(lambda x: 

                list(map(lambda y: list_a.count(y), options_b))

    , options_a))

print(output)

Results in output:

>>>[
    [2, 2, 0, 1],
    [2, 2, 0, 1],
    [2, 2, 0, 1],
    [2, 2, 0, 1]
   ]

Upvotes: 0

Views: 49

Answers (1)

Tomerikoo
Tomerikoo

Reputation: 19404

You are currently just counting each word from options_b in list_a again and again. What you are missing is to count it only if the number in list_b matches with the number from options_a. To do that you can use the zip() function to pack word-number together and then count the pairs:

pairs = list(zip(list_a, list_b))
output = [[pairs.count((word, num)) for word in options_b] for num in options_a]

print(output)
# [[1, 0, 0, 0], [1, 2, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]

I have changed to a list-comp because I believe it is more readable than the use of list(map(lambda ...))...


To avoid looping again and again (count will actually loop the list each time), you can use a Counter:

from collections import Counter

pair_counts = Counter(zip(list_a, list_b))
output = [[pair_counts[(word, num)] for word in options_b] for num in options_a]

print(output)
# [[1, 0, 0, 0], [1, 2, 0, 0], [0, 0, 0, 1], [0, 0, 0, 0]]

Upvotes: 2

Related Questions