为 Python 中的列表中的每个子列表返回大于 k 的数字索引

输入:查找子列表中大于 50 的数字的索引

a = [[10,40,90],[120,30,200],[70,90,100]]

期望的输出:

index_of_values_greater_than_50 = [[2][0,2][0,1,2]]


跃然一笑
浏览 113回答 2
2回答

慕姐8265434

output_list = []a = [[10,40,90],[120,30,200],[70,90,100]]for array in a:&nbsp; &nbsp;counter = 0&nbsp; &nbsp;new_list = []&nbsp; &nbsp;while counter < len(array):&nbsp; &nbsp; &nbsp; if array[counter] > 50:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;new_list.append(counter)&nbsp; &nbsp; &nbsp; counter += 1&nbsp; &nbsp;output_list.append(new_list)print(output_list)输出(根据需要):[[2], [0, 2], [0, 1, 2]]&nbsp; &nbsp;

慕少森

index_of_values_greater_than_50 = []for x in a:&nbsp;temp = [i for i, y in enumerate(x) if (y>50)&nbsp;index_of_values_greater_than_50.append(temp)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python