import random as rd
ListNumbers1 = []
List1 = []
for j in range(1000):
ListNumbers1 = rd.randint(1,10000)
ListNumbers1から50の最も高い数字を取得し、list1に追加するにはどうすればよいですか?pythonリスト内の複数の最大の数字
import random as rd
ListNumbers1 = []
List1 = []
for j in range(1000):
ListNumbers1 = rd.randint(1,10000)
ListNumbers1から50の最も高い数字を取得し、list1に追加するにはどうすればよいですか?pythonリスト内の複数の最大の数字
このようなものはありますか?そのような
List1.extend(sorted(ListNumbers1)[-50:])
私はそれを試して、それはint itntable wasntntだと言った –
@eunasonあなたのforループであなたの課題を修正する必要があります。 'ListNumbers1 = rd.randint(1,10000)'は 'ListNumbers1.append(rd.randint(1,10000))'でなければなりません。 –
(乱数を割り当てること、固定されている様子がわかり):
import random as rd
ListNumbers1 = []
List1 = []
for j in range(1000):
ListNumbers1.append(rd.randint(1,10000)) # fix: append each random element
ListNumbers1.sort() # sort the list, ascending order
List1 = ListNumbers1[-50:] # get last 50 elements of the list and assign it to List1
あなたは、プロセスであなたのリストを破壊し、あなたのループ内で何度も同じ値を代入しています。 append
...
ベターを使用します。
import random as rd
import heapq
highest_50 = heapq.nlargest(50,[rd.randint(1,10000) for _ in range(1000)])
print(highest_50)
結果:
[9994, 9983, 9983, 9968, 9934, 9925, 9913, 9912, 9909, 9909, 9902, 9889, 9884, 9880, 9811, 9794, 9793, 9792, 9765, 9756, 9750, 9748, 9738, 9737, 9709, 9707, 9704, 9700, 9691, 9686, 9635, 9618, 9617, 9605, 9604, 9593, 9586, 9584, 9573, 9569, 9569, 9557, 9531, 9528, 9522, 9438, 9438, 9431, 9402, 9400]
ああ、あなたはそれに私を打つ?申し訳ありませんが、私はこれを見ませんでした。 +1 –
import random as rd
List1 = sorted([rd.randint(1,10000) for j in range(1000)])[-50:]
直接50個の最高の数字を取得するためにheapq.nlargest
を使用し、その後、番号のリストの内包を作成ソートされたリストの理解の最後の50個の要素をスライスし、必要がない場合ListNumbers1
あなたはこれについていくつかの説明/文脈を含めることができますか? – EJoshuaS
そこに@EJoshuaS –
リスト内のn個の最大値を検索するには、2つの手順が必要です。1つは最も大きいものを、もう1つはnを最大にする手順です。 リストを並べ替えて最初に取り出すこともできますが、これは私のアプローチではありません。なぜなら、元のリストをそのまま維持して研究する必要があったからです。たとえば、選択した各数値のオフセットを知ることもできます。これは場合によっては有益です。あなたが抽出したい50くらいですので
from random import randint
import heapq
# create a list of 1000 random numbers
# take the negative, so the min heap does what we want
dataset = [-randint(1, 10000) for _ in range(1000)]
# O(n) function to impose the heap invariant
heapq.heapify(dataset)
# sorting is O(n log n)
# extracting from a heap is log n per item
# therefore taking the 50 biggest is much more efficent if
# we use a heap to extract only the ones we need
top50 = [-heapq.heappop(dataset) for _ in range(50)]
print top50
これは速いソリューションです:ホープこれは、 よろしく、楽しみのためだけに
を助け、私はより効率的なソリューションを持っている
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
def n_biggest(lis,howmany):
#this function returns the biggest of a list with its offset
def biggest_in_list(lis):
n=0
biggest=0
offset=0
for item in lis:
n=n+1
if (item>biggest):
biggest=item
offset=n-1
return[biggest,offset]
#now this is the part where we create the descending list
image=lis#the use of an image will prevent finding twice the same number
result_list=[]
if len(lis)<howmany:#this will prevent errors if list is too small
howmany=len(lis)
for i in range(howmany):
find1=biggest_in_list(image)
result_list.append(find1[0])
image.pop(find1[1])
return result_list
print n_biggest([5,4,6,10,233,422],3)#this line was for testing the above
合計入力サイズは1000未満です。私は変数の名前を変更しましたが、それは私の個人的な好みです。
は 'heapq.nlargest()'と同じではありませんか?おそらく、 –
。私はそれがどのように実装されているのかわかりませんが、これに従うことができ、アルゴリズムの性能保証を知ることができます。また、nlargestについてはわかりませんでしたが、おそらく最適化された組み込み関数なので、より速く実行できます。 –
'ListNumbers1'はリストではありません –
ループ内で' ListNumbers1'を繰り返し上書きします。それを無視する: 'List1 + = sorted(ListNumbers1 [-50:])'リスト内包表記を使うと良いでしょう。 'sorted([range(1000)のjに対してrd.randint(1,10000)]) ' –
繰り返し番号はどうですか?それとも50個のユニークな数字にしたいですか? – pylang