本帖最后由 阴阳神万物主 于 2021-6-20 13:17 编辑
集合是个好东西>>> a = [1,2,3]
>>> b = [2,3]
>>> c = [3]
>>> d = [ans for ans in [
[set(each) for each in [a,b,c]
if each][0].intersection(item) for item in [
set(each) for each in [a,b,c]
if each]
][-1]
]
>>> d
[3]
>>> c = []
>>> d = [ans for ans in [
[set(each) for each in [a,b,c]
if each][0].intersection(item) for item in [
set(each) for each in [a,b,c]
if each]
][-1]
]
>>> d
[2, 3]
>>>
这好像只是这个示例的特解,不是通解
这样就好多了>>> a = [1,2,3]
>>> b = [2,3]
>>> c = [3]
>>> temp = [
set(each) for each in [a,b,c]
if each
]
>>> d = [item for item in (set() if len(temp)==0
else
temp[0] if len(temp)==1
else
temp[0].intersection(temp[1]) if len(temp)==2
else
temp[0].intersection(temp[1]).intersection(temp[2])
)
]
>>> d
[3]
>>> c = []
>>> temp = [
set(each) for each in [a,b,c]
if each
]
>>> d = [item for item in (set() if len(temp)==0
else
temp[0] if len(temp)==1
else
temp[0].intersection(temp[1]) if len(temp)==2
else
temp[0].intersection(temp[1]).intersection(temp[2])
)
]
>>> d
[2, 3]
>>> c = [1,3]
>>> temp = [
set(each) for each in [a,b,c]
if each
]
>>> d = [item for item in (set() if len(temp)==0
else
temp[0] if len(temp)==1
else
temp[0].intersection(temp[1]) if len(temp)==2
else
temp[0].intersection(temp[1]).intersection(temp[2])
)
]
>>> d
[3]
>>>
|