lpt
lpt

Reputation: 975

compare list and get count

I have three lists

year= [2001, 2002, 2005, 2002, 2004, 2001, 2001, 2002, 2003, 2003, 2002, 2002, 2003, 2004, 2005, 2003, 2004, 2005, 2004, 2004 ]
indviduals= [12, 23, 24, 28,30, 15, 17, 18, 18, 19, 12, 15, 12, 12, 12, 15, 15, 15, 12, 12]
employers= ['a', 'b', 'c', 'd', 'e', 'a', 'a', 'b', 'b', 'c', 'b', 'a', 'c', 'd', 'e', 'a', 'a', 'a', 'a', 'b']

when I run the script below, i can get the individual employees across the lists. What I want to do is

a:[12, 15, 17, 15] for year 2001

If I can do this I think the getting count is just length.

for index, item in enumerate(year): 
    for i in np.unique(employers[index]):
        count=0
        #print(i)

        #j=indviduals[index]
        count +=1
        print(i)

Upvotes: 1

Views: 54

Answers (2)

keepAlive
keepAlive

Reputation: 6655

What about doing it for all your employers? using the buitin dict's method dict.fromkeys

d = dict.fromkeys(employers, ())
cond_year = 2001
for i,e,y in zip(indviduals, employers, year):
    if y == cond_year:
        d[e] = d[e] + (i,)

which prints

{'a': (12, 15, 17), 'b': (), 'c': (), 'd': (), 'e': ()}

Upvotes: 1

OneCricketeer
OneCricketeer

Reputation: 191728

You could use list comprehension

Find all individuals whose matching element in the employers list is an "a"

[individuals[i] for i, x in enumerate(employers) if x == 'a']

If you want to count it, then

sum(1 for x in employers if x == 'a') 

Otherwise, I would suggest using a single list of tuples that you can more easily filter by and not store parallel lists

Upvotes: 3

Related Questions