1

クラスのちょっとした雑学ゲームのハイスコア リストを作成しようとしています。ただし、辞書をランダムに印刷するだけでなく、印刷出力を取得できないように見えるため、問題が発生しています。このスニペットは、実際には完全なプログラムからのものではありません。これは、何かを台無しにしたくなかったため、機能を推論するための試みにすぎません。

scores = {'score1': {'initials': 'ywo',
                     'score': 20},
          'score2': {'initials': 'JRV',
                     'score': 18},
          'score3': {'initials': 'blh',
                     'score': 16},
          'score4': {'initials': 'yth',
                     'score': 15},
          'score5': {'initials': 'rtg',
                     'score': 12}}

total_score = 17


#iterates over the scores to see if new score and initials should be input
for i in (scores):
        if total_score > scores[i]['score']:
        scores[i]['initials'] = 'JKE'
        scores[i]['score'] = total_score
        break

#prints scores in a table like format rather than list
print("HIGH\tSCORES")
for i in scores:
    print(scores[i]['initials'], "\t", scores[i]['score'])

私の出力は毎回ランダムです。辞書を最高から最低の順に印刷したいだけです。

ywo    20 
JRV    18 
JKE    17

などなど

私が抱えているもう 1 つの問題は、他のスコアを辞書の下位に移動する方法がわからないことです。したがって、JKE のスコアが blh のスコアを置き換える場合、blh は辞書から削除されるだけでなく、score4 インデックスに移動し、score4 値は score5 などに移動します。アドバイスをいただければ幸いです。ありがとうございました!

4

7 に答える 7

1

これを少し複雑にしすぎているのではないかと思います (ただし、これは宿題のように聞こえるので、要件である可能性があります)。

私は次のようにアプローチします:

scores = {'YWO': 20,
          'BLH': 16,
          'YTH': 15,
          'JRV': 18,
          'RTG': 12,
         }


def display_scores(scores):
    for score, name in sorted(((scores[k], k) for k in scores, reverse=True):
        print(name, score)

次に、次の方法でプレーヤーとスコアを簡単に追加できます。

scores[initials] = scores.get(initials, 0) + 1 #or points or whatever
于 2013-10-15T18:50:07.843 に答える
0

ディクショナリは特定の順序を維持しませんが、OrderedDict を使用すると維持されます。

from collections import OrderedDict
d = OrderedDict([('first', 1),('second', 2),('third', 3)])
print d.items()

出力:

[('first', 1), ('second', 2), ('third', 3)]
于 2013-10-15T18:35:58.633 に答える
0

辞書のリストを使用すると、簡単にスコアを並べ替えることができ、たとえばベスト 3 のみを表示できます。リストの形式は [{score:player},...] のようになります。

于 2013-10-15T18:42:38.103 に答える
0

辞書は任意の順序で格納されます。出力を並べ替えたい場合は、手動で行う必要があります。たまたま正しい順序でソートされるキーscore1score2などがあるため、次のように実行できます。

for i in sorted(scores):
    print(scores[i]['initials'], "\t", scores[i]['score'])

ただし、コードは実際にはその辞書を適切に維持していないようです。スコアが を上回っているユーザーを見つけたら、スコアを に下げるのではなく、新しいスコアscore2に置き換えるだけです。score2score3

そもそもなぜスコアをこのように保存したいのかわかりません。順序を維持したい場合は、キー0をスルー4ではlistなくscore1スルーscore5で使用しないのはなぜdictですか? 例えば:

scores = [{'initials': 'ywo',
           'score': 20},
          {'initials': 'JRV',
           'score': 18},
          {'initials': 'blh',
           'score': 16},
          {'initials': 'yth',
           'score': 15},
          {'initials': 'rtg',
           'score': 12}]

for i, score in enumerate(scores):
    if total_score > score['score']:
        scores[i:i+1] = {'initials': 'JKE', 'score': total_score}
        del scores[5:]
        break

現在、それらは常にソートされています。

for score in scores:
    print(score['initials'], "\t", score['score'])

を使用すると、これをより効率的に行うことができますheapqheapqただし、 aは値自体を比較することによって値をソートしたままにするため、これは少し複雑になりますが、 で比較する必要がありますvalue['score']。Python のほとんどのソート関連の機能とは異なり、これを簡単にする機能heapqはありません。keyしたがって、手動で「装飾-並べ替え-装飾解除」作業を行う必要があります。

decorated_scores = [(score['score'], score) for score in scores]
heapify(decorated_scores)

ここで、新しいハイスコアを追加するには:

new_score = (total_score, {'initials': 'JRV', 'score': total_score})
dropped_off = heappushpop(decorated_scores, new_score)
if dropped_off == new_score:
    # didn't make the high scores
else:
    # did

SortedCollectionこれを簡単にするもう 1 つの方法は、ドキュメントで参照されているレシピのような自動並べ替えリスト、bisectまたは のようなライブラリを使用することblist.sortedlistです。

于 2013-10-15T18:37:50.407 に答える