Question

Python question: Suppose you have a dictionary of dictionaries, or a 2D dictionary, called banditos as...

Python question:

Suppose you have a dictionary of dictionaries, or a 2D dictionary, called banditos as follows: alonzo = {"age": 10, "height": 42, "weight": 175, "instrument":"fiddle" }

turing = {"age": 41, "height": 70, "weight": 160, "instrument": "theremin"}

bertha = {"age": 32, "height": 97, "weight": 587, "instrument": "cello"}

tinkerB = {"age":100, "height": 4, "weight": 0.5, "instrument": "cello"}

banditos = {"Alonzo": alonzo, "Turing": turing, "Bertha": bertha, "TinkerB": tinkerB}

We can access the sub-dictionary, or vector, of Alonzo's attributes as follows: banditos["Alonzo"] {"age": 10, "height": 42, "weight": 175, "instrument ": "fiddle" }

We can also return a specific feature of Alonzo's as: banditos["Alonzo"]["age"] 10

For the above dictionary, banditos, write a function that, when given the dictionary, returns a list of users who play a certain instrument. E.g., here's a sample run and its result: find_players(banditos, "cello") ["Bertha", "TinkerB"]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

def find_players(banditos, ins):

lst = []

for d in banditos:

    dict = banditos[d]

    for k in dict:

      if dict[k] == ins:

        lst.append(d)

return lst

alonzo = {"age": 10, "height": 42, "weight": 175, "instrument":"fiddle" }

turing = {"age": 41, "height": 70, "weight": 160, "instrument": "theremin"}

bertha = {"age": 32, "height": 97, "weight": 587, "instrument": "cello"}

tinkerB = {"age":100, "height": 4, "weight": 0.5, "instrument": "cello"}

banditos = {"Alonzo": alonzo, "Turing": turing, "Bertha": bertha, "TinkerB": tinkerB}


print(find_players(banditos,"cello"))

====================
SEE OUTPUT/ CODE FOR INDENTATION

PLEASE COMMENT if there is any concern.

=============================

Add a comment
Know the answer?
Add Answer to:
Python question: Suppose you have a dictionary of dictionaries, or a 2D dictionary, called banditos as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT