输入:
[['1', '2'], ['2', '3'], ['4', '5'], ['6', '7'], ['7', '8'], ['2', '6'], ['9', '10'], ['10', '2'], ['3', '5']]
输出(2个):
[0, 1, 5, 7] # 2 exists in these sublists which has 0, 1, 5, 7 indexes in main list
您可以通过列表理解清楚地做到这一点。
nested_list = [['1', '2'], ['2', '3'], ['4', '5'], ['6', '7'], ['7', '8'], ['2', '6'], ['9', '10'], ['10', '2'], ['3', '5']]
indices = [
index
for index, list_ in enumerate(nested_list)
if '2' in list_
]
print(indices)
输出量
[0, 1, 5, 7]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句