一个棘手的列表问题
abc 为三个列表变量求 a b c 列表中的相同项组成个新列表。但是abc有可能为空列表[].如果ab c 其中某项为空列表则不参与相同项的计算。
用列表生成式该如何写? 举个例子,题目没太明白 qq1151985918 发表于 2021-6-20 11:16
举个例子,题目没太明白
如a= b=c=
那么生成的列表为。。假如c=[] ,那么c不参与计算,生成的新列表为. 话说你为什么非要用列表生成式呢?多麻烦
>>> a =
>>> b =
>>> c =
>>>
>>> c = []
>>>
>>> 本帖最后由 阴阳神万物主 于 2021-6-20 13:17 编辑
集合是个好东西
>>> a =
>>> b =
>>> c =
>>> d = [ans for ans in [
if each].intersection(item) for item in [
set(each) for each in
if each]
][-1]
]
>>> d
>>> c = []
>>> d = [ans for ans in [
if each].intersection(item) for item in [
set(each) for each in
if each]
][-1]
]
>>> d
>>>
这好像只是这个示例的特解,不是通解
这样就好多了
>>> a =
>>> b =
>>> c =
>>> temp = [
set(each) for each in
if each
]
>>> d = [item for item in (set() if len(temp)==0
else
temp if len(temp)==1
else
temp.intersection(temp) if len(temp)==2
else
temp.intersection(temp).intersection(temp)
)
]
>>> d
>>> c = []
>>> temp = [
set(each) for each in
if each
]
>>> d = [item for item in (set() if len(temp)==0
else
temp if len(temp)==1
else
temp.intersection(temp) if len(temp)==2
else
temp.intersection(temp).intersection(temp)
)
]
>>> d
>>> c =
>>> temp = [
set(each) for each in
if each
]
>>> d = [item for item in (set() if len(temp)==0
else
temp if len(temp)==1
else
temp.intersection(temp) if len(temp)==2
else
temp.intersection(temp).intersection(temp)
)
]
>>> d
>>> a =
b =
c =
arr = None
if a and b and c:
arr =
print(arr)
页:
[1]