0

So I am having trouble with getting random item from random list inside a dictionary. The code I have right now

tasktotal = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect Wire","Electrical/Connect Wire","Cafeteria/Empty Garbage","Security/Connect Wire","Navigation/Navigate","Upper Engine/Fuel","Lower Engine/Fuel","Shield/Prime Shield","Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid","Admin/Connect Wire","Storage/Connect Wire"]

And then I did

task = dict(s.split('/') for s in task)

The problem with this is that I can only have one task in one location. I want to make it list in dictionary and also pick a random task from random location.

Dav_Did
  • 188
  • 10
  • 1
    The code you've shown is just a list. To choose a random item from a list, use `random.choice(my_list)`. We can't answer your question about dictionaries, because you haven't shown us an example. – John Gordon Oct 31 '20 at 21:55
  • That produces a dictionary of simple string pairs, none of which is a list. So again, we can't answer the question, because we don't have an example to work with. – John Gordon Oct 31 '20 at 22:02

4 Answers4

1

You can make a dictionary that maps keys to lists like this (alternatively you can use a defaultDict):

tasktotal = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect Wire","Electrical/Connect Wire","Cafeteria/Empty Garbage","Security/Connect Wire","Navigation/Navigate","Upper Engine/Fuel","Lower Engine/Fuel","Shield/Prime Shield","Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid","Admin/Connect Wire","Storage/Connect Wire"]

d = {}
for k, v in (s.split('/') for s in tasktotal):
    d.setdefault(k, []).append(v)

this will make d look like:

{'Medbay': ['Submit Scan', 'Submit Example'],
 'Cafeteria': ['Connect Wire', 'Empty Garbage'],
 'Electrical': ['Connect Wire'],
 'Security': ['Connect Wire'],
 'Navigation': ['Navigate'],
 'Upper Engine': ['Fuel'],
 'Lower Engine': ['Fuel'],
 'Shield': ['Prime Shield'],
 'Storage': ['Empty Garbage', 'Connect Wire'],
 'Admin': ['Swipe Card', 'Connect Wire'],
 'Weapon': ['Clear Asteroid']}

Given that you can get a random value from any list you like:

import random

random.choice(d['Medbay'])
# 'Submit Example' (or 'Submit Scan')
Mark
  • 90,562
  • 7
  • 108
  • 148
1

First you need to create a dictionary. Keys are unique, if you want to store multiple values under one key, store a list as value. Then draw from it:

tasktotal = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect Wire",
             "Electrical/Connect Wire","Cafeteria/Empty Garbage",
             "Security/Connect Wire","Navigation/Navigate",
             "Upper Engine/Fuel","Lower Engine/Fuel","Shield/Prime Shield",
             "Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid",
             "Admin/Connect Wire","Storage/Connect Wire"]

# import defaultdict
# d = defaultdict(list) 
# instead of d={} and then simply use d[key].append(...) it creates
# the list itself

data = {}
for task in tasktotal:
    key, value = task.split("/") 
    # either use plain dict and test key yourself
    if key in data:
        data[key].append(value)
    else:
        data[key] = [value]

    # or use setdefault 
    # d.setdefault(key,[]).append(value)

    # or  
    # d[key].append(...) with defaultdict(list)

print(data)

Then query randomly:

import random
# random key/value-list pair
key,values = random.choice(list(data.items()))
# random item from value-list
print(key, "->",  random.choice(values)) 

Output:

'edbay': ['Submit Scan', 'Submit Example'], 
'Cafeteria': ['Connect Wire', 'Empty Garbage'], 
'Electrical': ['Connect Wire'], 'Security': ['Connect Wire'], 
'Navigation': ['Navigate'], 'Upper Engine': ['Fuel'], 
'Lower Engine': ['Fuel'], 'Shield': ['Prime Shield'], 
'Storage': ['Empty Garbage', 'Connect Wire'], 
'Admin': ['Swipe Card', 'Connect Wire'], 'Weapon': ['Clear Asteroid']}


Lower Engine -> Fuel

You could have combined answers to these 2 questions to selfanswer yours:

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
1

You could use itertools.groupby

Note that the iterable you feed to groupby should be sorted on the same key you use for grouping, but in your case that is a simple sort…

In [12]: from itertools import groupby 
    ...: data = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect Wire","Electrical/Connect Wire","Cafeteria/Empty Garbage","Security/Connect Wire","Navigation/Navigate","Upper Engine/Fuel","Lower Engine/Fuel","Shield/Prime Shield","Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid","Admin/Connect Wire","Storage/Connect Wire"] 
    ...: data.sort() 
    ...: {k:list(s[1] for s in g) 
    ...:     for k, g in groupby((s.split('/') for s in data), lambda s:s[0])}    
Out[12]: 
{'Admin': ['Connect Wire', 'Swipe Card'],
 'Cafeteria': ['Connect Wire', 'Empty Garbage'],
 'Electrical': ['Connect Wire'],
 'Lower Engine': ['Fuel'],
 'Medbay': ['Submit Example', 'Submit Scan'],
 'Navigation': ['Navigate'],
 'Security': ['Connect Wire'],
 'Shield': ['Prime Shield'],
 'Storage': ['Connect Wire', 'Empty Garbage'],
 'Upper Engine': ['Fuel'],
 'Weapon': ['Clear Asteroid']}

In [13]: d = _                                                                                             

In [14]: from random import choice                                                                   

In [15]: rk = choice(list(d.keys()))                                                                 

In [16]: rk                                                                                          
Out[16]: 'Lower Engine'

In [17]: choice(d[rk])                                                                               
Out[17]: 'Fuel'

In its way, elegant…

gboffi
  • 22,939
  • 8
  • 54
  • 85
1
convert your code to dic to get value by key 

tasktotal = ["Medbay/Submit Scan","Medbay/Submit Example","Cafeteria/Connect 
Wire","Electrical/Connect Wire","Cafeteria/Empty Garbage",
"Security/Connect Wire","Navigation/Navigate","Upper Engine/Fuel","Lower 
Engine/Fuel","Shield/Prime Shield",
"Storage/Empty Garbage","Admin/Swipe Card","Weapon/Clear Asteroid","Admin/Connect 
Wire","Storage/Connect Wire"]

dic = {}
key = 0
for i in tasktotal:
i = i.split('/')
dic[key] = i[0]
key+=1
dic[key] = i[1]
key+=1
print(dic)