Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从python中的列表中获取公共值 我的名单如下:_Python_Python 3.x_List_Set - Fatal编程技术网

从python中的列表中获取公共值 我的名单如下:

从python中的列表中获取公共值 我的名单如下:,python,python-3.x,list,set,Python,Python 3.x,List,Set,我想从列表1和列表2中找出公共值。我写过 x = set(a[list_2015]).intersection(a[list_2016]) 但它抛出了一个错误: TypeError: list indices must be integers or slices, not tuple 我希望输出为: 这是一种方式: res = set(map(int, a[0]['list1'][0].split(','))) & \ set(map(int, a[1]['list2']

我想从列表1和列表2中找出公共值。我写过

x = set(a[list_2015]).intersection(a[list_2016])
但它抛出了一个错误:

TypeError: list indices must be integers or slices, not tuple
我希望输出为: 这是一种方式:

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}
注意
&
运算符是
集合的语法糖。交叉点

这是一种方法:

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}

注意
&
运算符是
集合的语法糖。交集

您的问题是列表由一个大字符串元素组成,而不是许多整数(如您所想)。所以基本上你首先需要把这个字符串转换成一个整数序列,然后你的代码使用交集就可以了!如果下面的解决方案有帮助,请考虑(左边的绿色刻度)。你的问题是你的列表由一个大的字符串元素组成,而不是许多整数(如你所想的那样)。所以基本上你首先需要把这个字符串转换成一个整数序列,然后你的代码使用交集就可以了!如果下面的解决方案有帮助,请考虑(左上绿色刻度)。
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}