我有一个(a,b)b 等于 1,2 或 3的元组列表。我想列出 a 的 where b == 2。如果该列表为空,我想列出所有 a 的 where b == 1。如果那也为空,那么我想列出所有 a 的列表b == 3。
现在我正在使用嵌套的 if 来完成这个:
sizeTwo = [tup[0] for tup in tupleList if tup[1] == 2]
if sizeTwo:
targetList = sizeTwo
else:
sizeOne = [tup[0] for tup in tupleList if tup[1] == 1]
if sizeOne:
targetList = sizeOne
else:
sizeThree = [tup[0] for tup in tupleList if tup[1] == 3]
if sizeThree:
targetList = sizeThree
else:
print(f"Error: no matching items in tupleList")
有没有“更清洁”的方法来实现这一目标?
 慕仙森
慕仙森 
					蝴蝶不菲
 
					呼啦一阵风
 随时随地看视频慕课网APP
随时随地看视频慕课网APP
相关分类