Python 2.** 从列表列表中找到 two-list 组合的所有可能交集的并集
Python 2.** Finding the union of all possible intersections of two-list combinations from a list of lists
免责声明:我正在自学 Python,因此我的每个问题都可能有一些简单的解决方案。感谢耐心等待!
我知道标题有点不清楚,所以我会尝试用一个例子来说明。
假设我们有一组交易:
txArray=[[u'1'],[u'2'],[u'2', u'3']]
目标是编写一个函数myIntersection(arrayOfLists)
,它首先计算txArray
中每对可能的列表的交集,然后取并集。
所以myIntersection(txArray)
应该return[u'2']
,因为:
int1=intersection([u'1'],[u'2'])=[]
int2=intersection([u'1'],[u'2', u'3'])=[]
int3=intersection([u'2'],[u'2', u'3'])=[u'2']
union=(int1 U int2 U int3)=[u'2']
到目前为止我尝试过的如下:
from itertools import combinations
'''
Pseudocode:
1) Generate all possible 2-combinations of the lists in txArray
2) Flatten the lists
3) If a value appears more than once in a 2-combination, add it to
list of intersections
4) Keep unique elements in list of intersections
'''
def myIntersection(arrayOfLists):
flat_list=[]
intersections=[]
combs=list(combinations(txArray,2))
for i in range(0, len(combs)):
flat_list.append([item for sublist in combs[i] for item in sublist])
for list in flat_list:
for element in list:
if list.count(element)>1:
if element not in intersections:
intersections.append(element)
return intersections
虽然它在 python command-line 界面中工作,但当我将其另存为 python 文件并 运行 时,我总是遇到这种方法的错误。
我的问题是:
1) 为什么当我 运行 它作为 python 文件时它不起作用?
2) 是否有更简洁、更 'pythonic' 的方法来做到这一点(可能使用列表理解)
3) 我确实考虑过使用集合来代替,但我想不出如何将 arrayofLists 的列表(在一般情况下)迭代地转换为集合。有这样做的简单语法吗?
非常感谢!
一个"more pythonic"解决方案:
import itertools
txArray=[[u'1'],[u'2'],[u'2', u'3']]
# generate all possible pairs from txArray, and intersect them
ix=[set(p[0]).intersection(p[1]) for p in itertools.combinations(txArray,2)]
# calculate the union of the list of sets
set.union(*ix)
你可以使用itertools.combinations
生成长度为2
的所有可能组合
In [232]: from itertools import combinations
In [233]: list(combinations(txArray, 2))
Out[233]: [(['1'], ['2']), (['1'], ['2', '3']), (['2'], ['2', '3'])]
然后您可以将每对列表变成一个 set
并对它们执行 intersection
得到一个集合列表
In [234]: intersections = [set(a).intersection(set(b)) for a, b in combinations(txArray, 2)]
In [235]: intersections
Out[235]: [set(), set(), {'2'}]
最后,您可以对集合执行 union
以从列表中解压所有集合
In [236]: set.union(*intersections)
Out[236]: {'2'}
此外,请注意解压缩组合 ([set(a).intersection(set(b)) for a, b in combinations(txArray, 2)]
) 比按索引 ([set(c[0]).intersection(set(c[1])) for c in combinations(txArray, 2)]
)
访问更易读 faster
免责声明:我正在自学 Python,因此我的每个问题都可能有一些简单的解决方案。感谢耐心等待!
我知道标题有点不清楚,所以我会尝试用一个例子来说明。
假设我们有一组交易:
txArray=[[u'1'],[u'2'],[u'2', u'3']]
目标是编写一个函数myIntersection(arrayOfLists)
,它首先计算txArray
中每对可能的列表的交集,然后取并集。
所以myIntersection(txArray)
应该return[u'2']
,因为:
int1=intersection([u'1'],[u'2'])=[]
int2=intersection([u'1'],[u'2', u'3'])=[]
int3=intersection([u'2'],[u'2', u'3'])=[u'2']
union=(int1 U int2 U int3)=[u'2']
到目前为止我尝试过的如下:
from itertools import combinations
'''
Pseudocode:
1) Generate all possible 2-combinations of the lists in txArray
2) Flatten the lists
3) If a value appears more than once in a 2-combination, add it to
list of intersections
4) Keep unique elements in list of intersections
'''
def myIntersection(arrayOfLists):
flat_list=[]
intersections=[]
combs=list(combinations(txArray,2))
for i in range(0, len(combs)):
flat_list.append([item for sublist in combs[i] for item in sublist])
for list in flat_list:
for element in list:
if list.count(element)>1:
if element not in intersections:
intersections.append(element)
return intersections
虽然它在 python command-line 界面中工作,但当我将其另存为 python 文件并 运行 时,我总是遇到这种方法的错误。
我的问题是: 1) 为什么当我 运行 它作为 python 文件时它不起作用?
2) 是否有更简洁、更 'pythonic' 的方法来做到这一点(可能使用列表理解)
3) 我确实考虑过使用集合来代替,但我想不出如何将 arrayofLists 的列表(在一般情况下)迭代地转换为集合。有这样做的简单语法吗?
非常感谢!
一个"more pythonic"解决方案:
import itertools
txArray=[[u'1'],[u'2'],[u'2', u'3']]
# generate all possible pairs from txArray, and intersect them
ix=[set(p[0]).intersection(p[1]) for p in itertools.combinations(txArray,2)]
# calculate the union of the list of sets
set.union(*ix)
你可以使用itertools.combinations
生成长度为2
In [232]: from itertools import combinations
In [233]: list(combinations(txArray, 2))
Out[233]: [(['1'], ['2']), (['1'], ['2', '3']), (['2'], ['2', '3'])]
然后您可以将每对列表变成一个 set
并对它们执行 intersection
得到一个集合列表
In [234]: intersections = [set(a).intersection(set(b)) for a, b in combinations(txArray, 2)]
In [235]: intersections
Out[235]: [set(), set(), {'2'}]
最后,您可以对集合执行 union
以从列表中解压所有集合
In [236]: set.union(*intersections)
Out[236]: {'2'}
此外,请注意解压缩组合 ([set(a).intersection(set(b)) for a, b in combinations(txArray, 2)]
) 比按索引 ([set(c[0]).intersection(set(c[1])) for c in combinations(txArray, 2)]
)