在子串列串列中,
lst = [[2,4,5,6], [0,1,3,7], [], ... , [8,9], [10, 11,12]]
找到具有特定元素的子串列,比如 9:
(知道所有元素都是唯一的)
output: [8, 9]
uj5u.com熱心網友回復:
next如果 9 在子串列中,您可以請求串列中的子串列,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]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/452136.html
標籤:python-3.x 列表
