2017-05-17 3 views
-1
import random as rd 

ListNumbers1 = [] 
List1 = [] 


for j in range(1000): 
    ListNumbers1 = rd.randint(1,10000) 

ListNumbers1から50の最も高い数字を取得し、list1に追加するにはどうすればよいですか?pythonリスト内の複数の最大の数字

+3

'ListNumbers1'はリストではありません –

+1

ループ内で' ListNumbers1'を繰り返し上書きします。それを無視する: 'List1 + = sorted(ListNumbers1 [-50:])'リスト内包表記を使うと良いでしょう。 'sorted([range(1000)のjに対してrd.randint(1,10000)]) ' –

+1

繰り返し番号はどうですか?それとも50個のユニークな数字にしたいですか? – pylang

答えて

2

このようなものはありますか?そのような

List1.extend(sorted(ListNumbers1)[-50:]) 
+0

私はそれを試して、それはint itntable wasntntだと言った –

+0

@eunasonあなたのforループであなたの課題を修正する必要があります。 'ListNumbers1 = rd.randint(1,10000)'は 'ListNumbers1.append(rd.randint(1,10000))'でなければなりません。 –

0

(乱数を割り当てること、固定されている様子がわかり):

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 
2

あなたは、プロセスであなたのリストを破壊し、あなたのループ内で何度も同じ値を代入しています。 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] 
+0

ああ、あなたはそれに私を打つ?申し訳ありませんが、私はこれを見ませんでした。 +1 –

0
import random as rd 
List1 = sorted([rd.randint(1,10000) for j in range(1000)])[-50:] 

直接50個の最高の数字を取得するためにheapq.nlargestを使用し、その後、番号のリストの内包を作成ソートされたリストの理解の最後の50個の要素をスライスし、必要がない場合ListNumbers1

+0

あなたはこれについていくつかの説明/文脈を含めることができますか? – EJoshuaS

+1

そこに@EJoshuaS –

0

リスト内の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 

これは速いソリューションです:ホープこれは、 よろしく、楽しみのためだけに

1

を助け、私はより効率的なソリューションを持っている

#!/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未満です。私は変数の名前を変更しましたが、それは私の個人的な好みです。

+0

は 'heapq.nlargest()'と同じではありませんか?おそらく、 –

+0

。私はそれがどのように実装されているのかわかりませんが、これに従うことができ、アルゴリズムの性能保証を知ることができます。また、nlargestについてはわかりませんでしたが、おそらく最適化された組み込み関数なので、より速く実行できます。 –

関連する問題