在子列表中,
lst = [[2,4,5,6], [0,1,3,7], [], ... , [8,9], [10, 11,12]]
找到包含特定元素的子列表,例如9:
(知道所有元素都是独一无二的)
output: [8, 9]
你可以要求 next 如果9在带有 None 如果找不到:
next
None
lst = [[2,4,5,6], [0,1,3,7], [], [8,9], [10, 11,12]] next((l for l in lst if 9 in l), None) # [8, 9]