从python列表中获取常用值 [英] Get common values out of a list in python
本文介绍了从python列表中获取常用值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
a=[{'list1': ['35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52']},
{'list2': ['1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52']}]
我想从list1和list2中找出共同的值.我写了
I want to get out the common values from both list1 and list2. I have written
x = set(a[list_2015]).intersection(a[list_2016])
但是它抛出错误:
TypeError: list indices must be integers or slices, not tuple
我希望输出为:
{35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52}
推荐答案
这是一种方法:
res = set(map(int, a[0]['list1'][0].split(','))) & \
set(map(int, a[1]['list2'][0].split(',')))
{35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52}
注意&
运算符是set.intersection
的语法糖.
Note &
operator is syntactic sugar for set.intersection
.
这篇关于从python列表中获取常用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文