我如何找到等于其索引值的數字并按排序順序列印它們。給定一個數字 n 后跟 n 個數字
例如:輸入 6 7 3 3 4 5 輸出 3 4 5
uj5u.com熱心網友回復:
完美的用例enumerate
print ([n for i, n in enumerate([6,7,3,3,4,5]) if i == n])
i是你的索引
n是你的串列條目
uj5u.com熱心網友回復:
你可以做一個listcomp:
output = [x for x in range(len(input)) if input[x]==x]
uj5u.com熱心網友回復:
對不起,它在js中,但邏輯相同
讓 arr = "輸入 67 3 3 4 5 輸出 3 4 5"
let joined = arr.split("")
let box = []
let arrayIndex = joined.forEach((item,idx)=>{
if(idx == item){
box.push(item)
}
})
console.log(box.sort())
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/482225.html
