Using class function to create/call on specific dictionary key and their items

Question:

I am attempting to use the class property to create specific dictionaries and call on random key/items. Here’s my current code that calls on a specific sport from the dictionary comprehension:

import random

# list dictionary
result = {'Peter': ['Male', 'Basketball'], 'Carlos': ['Male', 'Tennis'], 'Angela': ['Female', 'Golf'], 'David': ['Male', 'Basketball'], 'Kaitlin': ['Female', 'Golf'], 'Marco': ['Male', 'Tennis'], 'Andrew': ['Male', 'Golf'], 'Carmen': ['Female', 'Basketball']}

#list desired sport
sportChoice = input("Enter desired sport workout: ")

# filter dictionary for the sportChoice variable
parsedData = [
    'n'.join([f"Name: {name}", f"Gender: {info[0]}", f"Sport: {info[1]}"])
    for name, info in result.items()
    if info[1] == "{}".format(sportChoice)
]

# grab a random dictionary key and item from the filtered list
intList = []


for numbers in range(len(parsedData)):
    intList.append(numbers)
print(intList)
print(''.join(parsedData[int(random.choice(intList))]))

The code above enables me to write a certain sport and get a random key with it’s entries in it. However, this only works for calling on a desired sport. I’d like the code to work for choosing a specific gender (genderChoice) as well but I’m having trouble implementing into the program. Here’s what I have now:

import random

# list dictionary
result = {'Peter': ['Male', 'Basketball'], 'Carlos': ['Male', 'Tennis'], 'Angela': ['Female', 'Golf'], 'David': ['Male', 'Basketball'], 'Kaitlin': ['Female', 'Golf'], 'Marco': ['Male', 'Tennis'], 'Andrew': ['Male', 'Golf'], 'Carmen': ['Female', 'Basketball']}

# list desired sport
sportChoice = input("Enter desired sport workout: ")
genderChoice = input("Enter desired Gender. If no preference, leave blank: ")

# create if statement for choice of gender
if genderChoice == "":
    genderChoice == "Male"

# filter dictionary for the variable sportChoice
parsedData = [
    'n'.join([f"Name: {name}", f"Gender: {info[0]}", f"Sport: {info[1]}"])
    for name, info in result.items()
    if info[1] == "{}".format(sportChoice)
]

# grab a random dictionary key and item from the filtered list
intList = []


for numbers in range(len(parsedData)):
    intList.append(numbers)
print(intList)
print(''.join(parsedData[int(random.choice(intList))]))

I feel I’m not doing this as efficiently as it could be. Hence me mentioning the class property. Or maybe some other function would work better. Here’s some really bad code of me attempting to do this with a class:

result = {'Peter': ['Male', 'Basketball'], 'Carlos': ['Male', 'Tennis'], 'Angela': ['Female', 'Golf'], 'David': ['Male', 'Basketball'], 'Kaitlin': ['Female', 'Golf'], 'Marco': ['Male', 'Tennis'], 'Andrew': ['Male', 'Golf'], 'Carmen': ['Female', 'Basketball']}
intList = []

class CreateSportList():
    def sport(self):
        parsedData = [
            'n'.join([f"Name: {name}", f"Gender: {info[0]}", f"Sport: {info[1]}"])
            for name, info in result.items()
            if info[1] == "{}".self

    for numbers in range(len(parsedData)):
        intList.append(numbers)
    print(intList)
    print(''.join(parsedData[int(random.choice(intList))]))

CreateSportList(Tennis, Female)

Any help/direction would be appreciated!

Here’s the desired output:

Enter desired sport workout: Golf
Enter desired Gender. If no preference, leave blank: Female
Name: Angela
Gender: Female
Sport: Golf
Asked By: bbsmfb

||

Answers:

First item, you have:

if genderChoice == "":
    genderChoice == "Male"

That second line should use =, not ==.

You have a bunch of unnecessary code here. This does what you want:

import random

# Set up database.
result = {'Peter': ['Male', 'Basketball'], 'Carlos': ['Male', 'Tennis'], 'Angela': ['Female', 'Golf'], 'David': ['Male', 'Basketball'], 'Kaitlin': ['Female', 'Golf'], 'Marco': ['Male', 'Tennis'], 'Andrew': ['Male', 'Golf'], 'Carmen': ['Female', 'Basketball']}

# list desired sport
sportChoice = input("Enter desired sport workout: ")
genderChoice = input("Enter desired Gender. If no preference, leave blank: ")

if not genderChoice:
    genderChoice = "Male"

parsedData = [
    'n'.join([f"Name: {name}", f"Gender: {info[0]}", f"Sport: {info[1]}"])
    for name, info in result.items()
    if info[1] == sportChoice and info[0] == genderChoice
]

print(random.choice(parsedData))

Note that "{}".format(sportChoce) is exactly the same as sportChoice. Note that you don’t need to create an array of list indexes. Just let random.choice pick from the list.

You MIGHT consider creating a class Person to hold their gender and sport, but that doesn’t really gain you very much. You’ll still need to search through them one by one.

Answered By: Tim Roberts
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.