2017-05-10 9 views
0

私はdictからランダムなキー/値を返す関数を持っています。私は私のアプリを通じて数回、この関数を呼び出す関数はランダムな値を返します - 毎回異なっていることを保証します

def heroes(): 
    hero, attribute = random.choice(list(heroes.items())) 
    return(hero, attribute) 

、私はそれぞれの呼び出しは、以前のものとは異なるランダムな値を返すことを保証する方法を探しています、可能性を簡単に行うためにということですか?ここで

+0

もしそれがランダムであれば、現在のものが前のものと等しくないという保証はありません。しかし、あなたが戻ってきたアイテムを削除することができます。 – e4c5

+0

値が異なる可能性がある場合は、関数によって返されたすべての値を追跡し、新しく作成されたセットがすでに生成されているかどうかをチェックする必要があります。 – ForceBru

答えて

1

は辞書を取り、それのためにランダムなイテレータを作成する関数です:

import random 

def rand_items(d): 
    def g(): 
     d_items = list(d.items()) 
     random.shuffle(d_items) 
     for i in d_items: 
      yield i 
    return g() 

#tested like: 

hero = {'batman':'rope','spiderman':'web', 'superman':'cape'} 

heroes = rand_items(hero) 

for h in heroes: 
    print(h) 

いずれかを実行:

('spiderman', 'web') 
('superman', 'cape') 
('batman', 'rope') 

一度に英雄1を得ることができます。あなたは自分の関数呼び出しの代わりにnext(heroes)を使用英雄を必要とする時はいつでも次に

heroes = rand_items(hero) 

:を初期化します。

0

私はデコレータを使用することをお勧めします。それはあなたが英雄を通して無限にループすることを可能にします。たとえば:あなたは@except_last_oneコメント場合は、機能を

import random 

# In this version heroes are global 
HEROES = {'a':10, 'b':20, 'c':30, 'd':40} 

def except_last_one(hero_func): 
    # storage for the last hero 
    last_hero = [None] 
    def new_hero(): 
     # define a new hero (but not the last hero) 
     last_hero[0] = hero_func([k for k in HEROES.keys() if k != last_hero[0]])[0] 
     return (last_hero[0], HEROES[last_hero[0]]) 
    return new_hero 

@except_last_one 
def hero(heroes=HEROES.keys()): 
    hero = random.choice(heroes) 
    return (hero, HEROES[hero]) 

得るあなたの機能heroes()に等しいです。そして、私はたぶんあなたがただ一つのヒーローを持っている場合に例外を追加するでしょう:

 try: 
      last_hero[0] = hero_func([k for k in HEROES.keys() if k != last_hero[0]])[0] 
     except IndexError: 
      print 'Sorry, no heroes for you.' 
      return (None, None) 
関連する問題