比较列表并获得计数

我有三个清单


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']

当我运行下面的脚本时,我可以在列表中找到各个员工。我想做的是


a:[12, 15, 17, 15] 2001年


如果我能做到这一点,我认为获取计数只是长度。


for index, item in enumerate(year): 

    for i in np.unique(employers[index]):

        count=0

        #print(i)


        #j=indviduals[index]

        count +=1

        print(i)


哆啦的时光机
浏览 138回答 2
2回答

慕哥9229398

为你所有的雇主做这件事怎么样?使用buitin dict的方法dict.fromkeysd = dict.fromkeys(employers, ())cond_year = 2001for i,e,y in zip(indviduals, employers, year):    if y == cond_year:        d[e] = d[e] + (i,)哪个打印{'a': (12, 15, 17), 'b': (), 'c': (), 'd': (), 'e': ()}

青春有我

你可以使用列表理解查找雇主列表中匹配元素为“a”的所有个人[individuals[i] for i, x in enumerate(employers) if x == 'a']如果你想计算它,那么sum(1 for x in employers if x == 'a')否则,我建议使用单个元组列表,您可以更轻松地过滤而不是存储并行列表
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python