简体   繁体   中英

Choosing random values from a dictionary and afterwards ordering it

I am trying to make a random class generator for COD WW2 and I'm almost done but I have one problem. I'm trying to choose random scorestreaks (in which I succeed), but when my program prints the scorestreaks they are not ordered from cheapest to most expensive. I understand that this is normally also not the case but I would like it differently.

So I have been thinking and I think that dictionaries would be the key but I'm a beginner to Python and I haven't really used dictionaries before.

This is my list with scorestreaks:

scorestreaks = ["Molotov Cocktail", "Recon Aircraft", "Counter Recon 
Aircraft", "Care Package", "Fighter Pilot", "Glide Bomb", "Flamethrower", 
"Mortar Strike", "Artillery Barrage", "Flak Guns", "Emergency Airdrop", 
"Fire Bombing Run", "Paratroopers", "Carpet Bombing", "Ball Turret Gunner"]

And this is my random choosing thing:

ss1 = random.choice(scorestreaks)
ss2 = random.choice(scorestreaks)
ss3 = random.choice(scorestreaks)
if ss1 == ss2 or ss1 == ss3:
    ss1 = random.choice(scorestreaks)
    if ss1 == ss2 or ss1 == ss3:
        ss1 = random.choice(scorestreaks)
elif ss2 == ss3:
    ss2 = random.choice(scorestreaks)
    if ss2 == ss3 or ss2 == ss1:
        ss2 = random.choice(scorestreaks)

print "Scorestreaks: %s, %s, %s" %(ss1, ss2, ss3)

So idealy I would like to have the scorestreaks ordered when they are printed from cheapest to most expensive. Thank you for your answer.

EDIT:

I made a dictionary (I hope I made it the right way) containing the prices of the scorestreaks:

scorestreaks_prices = {"Molotov Cocktail": 300, "Recon Aircraft": 500, "Counter Recon Aircraft": 525, "Care Package": 575, "Fighter Pilot": 625, "Glide Bomb": 650, "Flamethrower": 700, "Mortar Strike": 750, "Artillery Barrage": 850, "Flak Guns": 950, "Emergency Airdrop": 1000, "Fire Bombing Run": 1050, "Paratroopers": 1250, "Carpet Bombing": 1400, "Ball Turret Gunner": 1700}

So to make it clear: When I choose three random scorestreaks It would print something like "Molotov Cocktail", "Counter Recon Aircraft", "Recon Aircraft". This instead of: "Molotov Cocktail", "Recon Aircraft", "Counter Recon Aircraft", which has been ordered on price. So how do I do this?

There are numerous possible ways of doing this but essentially you want to:

  • take a random subset of the items
  • sort that subset by the item prices
  • get the item names as a tuple which you can use in your string formatting expression

Here's one solution:

scorestreaks_prices = {"Molotov Cocktail": 300, "Recon Aircraft": 500, 
    "Counter Recon Aircraft": 525, "Care Package": 575, "Fighter Pilot": 625, 
    "Glide Bomb": 650, "Flamethrower": 700, "Mortar Strike": 750, "Artillery Barrage": 850, 
    "Flak Guns": 950, "Emergency Airdrop": 1000, "Fire Bombing Run": 1050, 
    "Paratroopers": 1250, "Carpet Bombing": 1400, "Ball Turret Gunner": 1700}

# Use a dict comprehension to create a random 3-item subset of the full dict
ssItems = {item:scorestreaks_prices[item] for item in random.sample(scorestreaks_prices.keys(), 3)}
# Sort the subset dict by the value (i.e. price); the result is a list of the keys (i.e. the item names)
ss = sorted(ssItems, key=lambda x:ssItems[x])
# Format the item list into a string
print("Scorestreaks: %s, %s, %s" %tuple(ss))

To understand how this works, experiment with dict comprehensions (and the slightly easier to grasp list comprehensions) at the Python prompt. For example try {x:len(x) for x in ['foo', 'bar', 'hello', 'z']} : what do you get and why?

The lambda x:ssItems[x] is also a bit obscure for beginners but it simply means "a function which returns the value of key x from the dict ssItems ". By using that function as the key to sort by, we sort by the prices given for each item.

Your method for randomly choosing different items might still return the same item twice. Using random.sample guarantees that the three items will be different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM