私は研究で大きなデータセットを扱います。リストまたはナンシー配列の特定の要素を複製する
Numpy配列の要素を複製する必要があります。以下のコードはこれを実現していますが、より効率的に操作を実行する関数がNumpyにありますか?
"""
Example output
>>> (executing file "example.py")
Choose a number between 1 and 10:
2
Choose number of repetitions:
9
Your output array is:
[1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10]
>>>
"""
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y = int(input('Choose the number you want to repeat (1-10):\n'))
repetitions = int(input('Choose number of repetitions:\n'))
output = []
for i in range(len(x)):
if x[i] != y:
output.append(x[i])
else:
for j in range(repetitions):
output.append(x[i])
print('Your output array is:\n', output)
これはまさに私が探していたものです!本当にありがとう! 私はリストをスライスする必要があると思っていましたが、最もエレガントなアプローチではわかりませんでした。 :) – tionichm
@JanCostandius質問に投稿された特定の 'x'のより洗練された解決策については、編集をチェックしてください。 – Divakar