提问人:John Kuldeep Roshan Kerketta 提问时间:8/2/2020 最后编辑:Gavin WongJohn Kuldeep Roshan Kerketta 更新时间:8/2/2020 访问量:118
在 Python 中,列表中每个子列表返回大于 k 的数字索引
Return index of numbers greater than k for each of sublists in list in Python
问:
输入: 在子列表中查找大于 50 的数字索引
a = [[10,40,90],[120,30,200],[70,90,100]]
所需输出:
index_of_values_greater_than_50 = [[2][0,2][0,1,2]]
答:
2赞
Gavin Wong
8/2/2020
#1
output_list = []
a = [[10,40,90],[120,30,200],[70,90,100]]
for array in a:
counter = 0
new_list = []
while counter < len(array):
if array[counter] > 50:
new_list.append(counter)
counter += 1
output_list.append(new_list)
print(output_list)
输出(根据需要):
[[2], [0, 2], [0, 1, 2]]
0赞
John Kuldeep Roshan Kerketta
8/2/2020
#2
index_of_values_greater_than_50 = []
for x in a:
temp = [i for i, y in enumerate(x) if (y>50)
index_of_values_greater_than_50.append(temp)
评论
0赞
FluffyKitten
8/2/2020
欢迎来到 Stack Overflow。在 Stack Overflow 上不鼓励使用纯代码答案,因为它们没有解释它如何解决问题。请编辑您的答案以解释代码的作用以及它如何回答问题,以便它对其他用户以及 OP 也很有用。
评论