print random from list with no duplicate - list

I have 3 lists called animal, adjectives and group_members_b.
The group_members_b list has 5 names and need to print them out to random selcetion from animal and adjectives list.
When i use the code below i get duplicates of the name. How would i get the 5 names printed so each name is used once?
for i in range (0,5):
animal = random.choice(animal_types)
adjectives = random.choice(adjectives_list)
name = random.choice(group_members_b)
print (name, "is a", adjectives, animal)

Try to see Fisher-Yates shuffle. It's used for list randomization with no repetitions.
I hope that helps.

Related

Dungeons and Dragons Character Sheet generator using Python

For fun I'm trying to create a character generator sheet for Dungeons and Dragons. I've got the program to randomly roll for my strength, charisma etc.
Now I want to be able to ask the user, "What type of weapon do you want to use?" Get_Weapon_Choice, then pull up a list of that weapon type. I've tried creating a list weapon_Choices = ['Bow', 'Sword', ]. Then I created 2 other lists, bow = ['short', 'long', 'crossbow'] and swords = ['short', 'long'] I know how to get input from the user, but I don't know how to take that input and print the list, I'm printing the variable name.
chooseWeapon = input('What type of weapon would you like? Bow or Sword?')
How do I use chooseWeapon to compare to weapon_Choices to make sure they didn't enter something like Spells, then use chooseWeapon to print either the bow[] list or the swords[] list?
Do I need to use MySQL along with Python? and create tables and then search the tables instead of lists?
You can create a dictionary:
weapons = {"bow": ['short', 'long', 'crossbow'], "sword": ['short', 'long']}
# prompt user and convert to lowercase (our dict consists of lowercase strings)
chooseWeapon = input('What type of weapon would you like? Bow or Sword?').lower()
if chooseWeapon in weapons: # checks if the input is one of the keys in our dict
print(f'Available {chooseWeapon}s: {weapons[chooseWeapon]}')

keyword inspection based on words present in multiple lists

I have a dictionary similar to this:
countries = ["usa", "france", "japan", "china", "germany"]
fruits = ["mango", "apple", "passion-fruit", "durion", "bananna"]
cf_dict = {k:v for k,v in zip(["countries", "fruits"], [countries, fruits])}
and I also have a list of strings similar to this:
docs = ["mango is a fruit that is very different from Apple","I like to travel, last year I was in Germany but I like France.it was lovely"]
I would like to inspect the docs and see if each string contains any of the keywords in any of the lists(the values of cf_dict are lists) in cf_dict, and if they are present then return the corresponding key(based on values) for that string(strings in docs) as output.
so for instance, if I inspect the list docs the output will be [fruits, countries]
something similar to this answer but this checks only one list, however, I would like to check multiple lists.
The following returns a dict of sets in case a string matches values in more than one list (e.g. 'apple grows in USA' should be mapped to {'fruits', 'countries'}).
print({s: {k for k, l in cf_dict.items() for w in l if w in s.lower()} for s in docs})
This outputs:
{'mango is a fruit that is very different from Apple': {'fruits'}, 'I like to travel, last year I was in Germany but I like France.it was lovely': {'countries'}}

How to subtract unkown strings from list in python

I am trying to write a program that you say to it, from now on call me Jason, then will convert it into a list and subtract everything but Jason from the list. I managed to make this but, i want it to subtract words that aren't in there but would be able to if they were there.
You haven't posted any code, so here is how I would do it.
names = set(['John','Jason','Jim'])
callme = 'Jason'
names.intersection(set([callme]))
Alternatively, with iterators
names = ['John','Jason','Jim']
callme = ['Jason']
[N for N in names if N in callme]

Applying regexp and finding the highest number in a list

I have got a list of different names. I have a script that prints out the names from the list.
req=urllib2.Request('http://some.api.com/')
req.add_header('AUTHORIZATION', 'Token token=hash')
response = urllib2.urlopen(req).read()
json_content = json.loads(response)
for name in json_content:
print name['name']
Output:
Thomas001
Thomas002
Alice001
Ben001
Thomas120
I need to find the max number that comes with the name Thomas. Is there a simple way to to apply regexp for all the elements that contain "Thomas" and then apply max(list) to them? The only way that I have came up with is to go through each element in the list, match regexp for Thomas, then strip the letters and put the remaining numbers to a new list, but this seems pretty bulky.
You don't need regular expressions, and you don't need sorting. As you said, max() is fine. To be safe in case the list contains names like "Thomasson123", you can use:
names = ((x['name'][:6], x['name'][6:]) for x in json_content)
max(int(b) for a, b in names if a == 'Thomas' and b.isdigit())
The first assignment creates a generator expression, so there will be only one pass over the sequence to find the maximum.
You don't need to go for regex. Just store the results in a list and then apply sorted function on that.
>>> l = ['Thomas001',
'homas002',
'Alice001',
'Ben001',
'Thomas120']
>>> [i for i in sorted(l) if i.startswith('Thomas')][-1]
'Thomas120'

How to search and replace keywords in strings in python from multiple categories?

Assume I have a string which reads:
'''
Looking closely we find Pepsi and Coca-cola have been the two two biggest brands of soda in the world for the past four years.
'''
and I want to represent mapping of words to its classes as :
classes={"NAME":["pepsi","coca-cola", "james","jill"...],"CATEGORY":["soda","food","automobile"....],"NUMBER":["one","two","three","four"....]}
So that at the end I want to have the original string as :
Looking closely we find NAME and NAME have been the two biggest brands of CATEGORY in the world for the past NUMBER years
for a simple dict like :
rep = {"NAME": "pepsi", "CATEGORY": "soda"....}
I can replace the words for above dict but how do I do that if there more than one word per key?
This is what I have so far:
stringh=sentence.lower()
for i,j in rep.items():
stringh = stringh.replace(j, i)
print stringh
Iterating through the list
stringh=sentence.lower()
for i,j in rep.items():
for k in j:
stringh = stringh.replace(k, i)
print stringh