Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Python

erikpc43
 Share

If a dictionary is given like :  names = { "Peter": ("computer", "headphones", "chair),

                                                              "John": ("table", computer"),

                                                              "Ken": ( "headphones"),

                                                              "Ben": ("chair", "table" )    }

Now how can i return a set that will contain keys with the same values? it should be in alphabetical order by names like in this case {  ("John", "Peter"), ("Ken", "Peter"), ("Ben", "Peter"), ("Ben", "John") }. 

Thanks

Link to comment
Share on other sites

Link to post
Share on other sites

We'll help you with specific questions, but you need to show us what you have so far and have a specific issue in mind. You've got to show that you at least tried something before we can help.

Gaming build:

CPU: i7-7700k (5.0ghz, 1.312v)

GPU(s): Asus Strix 1080ti OC (~2063mhz)

Memory: 32GB (4x8) DDR4 G.Skill TridentZ RGB 3000mhz

Motherboard: Asus Prime z270-AR

PSU: Seasonic Prime Titanium 850W

Cooler: Custom water loop (420mm rad + 360mm rad)

Case: Be quiet! Dark base pro 900 (silver)
Primary storage: Samsung 960 evo m.2 SSD (500gb)

Secondary storage: Samsung 850 evo SSD (250gb)

 

Server build:

OS: Ubuntu server 16.04 LTS (though will probably upgrade to 17.04 for better ryzen support)

CPU: Ryzen R7 1700x

Memory: Ballistix Sport LT 16GB

Motherboard: Asrock B350 m4 pro

PSU: Corsair CX550M

Cooler: Cooler master hyper 212 evo

Storage: 2TB WD Red x1, 128gb OCZ SSD for OS

Case: HAF 932 adv

 

Link to comment
Share on other sites

Link to post
Share on other sites

Definitely seems like homework! But I'll give you a few pointers.

 

You're looking to bucket (i.e. group) people who have the same items. A dict will do. Iterate through the given dictionary, for each key, iterate through the items. Set this as your key for your new dictionary. You'll need a way to store this collection of people grouped by item, I'll leave that up to you.

After you've grouped by item, simply iterate once more and output the list of people for each key.

Link to comment
Share on other sites

Link to post
Share on other sites

@erikpc43

https://docs.python.org/3/library/stdtypes.html#dict

 

Here you can find ressources on python dictionaries idk if I am too late but its worth working through the methods contained in the dict class.

Do that and you got the solution in no time :) 

If you have some questions concerning notation or syntax you could send me an message. ;)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share


×