Ask your own question, for FREE!
Computer Science 21 Online
OpenStudy (anonymous):

I have one question about writing a code on dictionaries i have done the coding but i am confused on one part this is what it says the parameter name is sort_by_value: "(dict of {immutable_type:int}) -> list" and the description is "Return a list of keys in the dictionary sorted in ascending order according to their values. Keys with identical values might be sorted in any order. "

OpenStudy (anonymous):

my code that i already wrote is def sort_by_value(dict): """ (dict of {immutable_type:int}) -> list """ L = [] for x in dict.values(): L.append(x) L.sort() keyList= [] for x in L: keyList.append(dict.get()) if print L

OpenStudy (anonymous):

like before where i introduced KeyList = [] everything works BUT I am getting the values in accending order not the keys its like say for example I have nd = {} nd['john'] = 109090 nd['albert'] = 1900 nd['Tim'] = 18000 sort_by_value(nd) I get the list [1900, 18000, 109090] but I need to get the keys associated with those values like I want ['albert', 'Tim', 'john'] Please helpp

OpenStudy (anonymous):

@shandelman

OpenStudy (anonymous):

@shandelman

OpenStudy (anonymous):

like if you put that code in dont put this one in for x in L: keyList.append(dict.get()) if i was trying to get the keys in but failed (N)

OpenStudy (anonymous):

Believe it or not, you can write this function in one line of code. No loop necessary. Here's a couple of hints: 1) There is a sorted() function that works for ANY iterable type (lists, tuples, dictionaries, strings) that returns a sorted *list* (even if the original type wasn't a list). 2) The sorted() function takes an optional argument called key, and this is how you tell the function HOW you want to sort the elements...you give it a different function that gets a value from each element. For example, let's say I had the list lst = ["bobcat", "aardvark", "dog", "caterpillar"]. If I did sorted(lst), I would get ["aardvark", "bobcat", "caterpillar", "dog"] which are the elements sorted in alphabetical order. However, let's say I wanted to sort them by length. I would do this: sorted(lst, key = len) I would get ["dog", "bobcat", "aardvark", "caterpillar"]. This goes through each element, calls len on each element to find the length, and sorts using that number. Notice I did NOT put parentheses next to len. It doesn't need them. The only question you now have to answer is "What function takes an element of a dictionary and returns the value of that element? And I know you already know this.

OpenStudy (anonymous):

but that doesnt answer my question though

OpenStudy (anonymous):

like so my code was wrong and have to do it again?

OpenStudy (anonymous):

Let me look more closely at your code. I was just giving you a way that you could have written the same program in one line of code. I can figure out what's wrong with your current code if that is what you want.

OpenStudy (anonymous):

like i when I run the code right now I just get [1900, 18000, 109090]

OpenStudy (anonymous):

I see what you were trying to do. You created a sorted list of values, then tried to go through the list of values and get the key associated with that value. Unfortunately, it doesn't quite work like that. Values are associated with keys, not the other way around. Imagine if I had a dictionary like this: {'a':0,'b':0,'c':0,'d':0} and I asked you what key goes with the value 0...obviously, there's not one answer to that question. The function dict.get() by the way takes a KEY and gets a VALUE...and you need to actually give it a key...I don't think giving get() no input will work. I suggest you try it my way. You'll find it requires a lot less code, and I'm not sure the way you're doing it is going to end up working.

OpenStudy (anonymous):

hmm so there isnt any code that i could put after L.sort() and make the computer give me the keys instead?

OpenStudy (anonymous):

because i kind of dont get what your paragraph meant :$

OpenStudy (anonymous):

You know the line that says: sorted(lst,key=len) that I used to sort a list by the length of each of its items? There's a code that is about as simple to sort a dictionary by the value of its keys. sorted(dic,key=__________) You just need to fill in the blank with the function that takes a key and gets a value.

OpenStudy (anonymous):

And, no, I don't believe there is a code to put after L.sort() because you can't get a list of keys from a list of values since a value can be associated with many different keys.

OpenStudy (rsmith6559):

Take a look at: http://docs.python.org/library/stdtypes.html#mapping-types-dict Most of what you need to do is implemented in the dictionary.

OpenStudy (anonymous):

Here's one more example why your method doesn't quite work. I'm sorry if I'm beating a dead horse. Let's say I have a class of Geometry students. Some are 15 years old, some are 16, and some are 17. I write a dictionary to take their names and gives each name their age value. So it might look like this: ages = {'Bob':16,'Julie':15,'Jennifer':16,'A.J.':17,'Calvin':15} and so on. Imagine there are 12 different names in there. Now let's say I wrote a function that takes the dictionary, pulls out all the values, and sorts a list with those values. This is, in essence, what you did. The new list would look something like this: {15,15,15,15,15,16,16,16,16,17,17,17}. Now I want to go through this list of values and build a new list with the keys associated with those values. So I look at the first element and say "Which key was associated with that?" And the answer is...well, Julie was, and Calvin, and several more students. Now I guess it doesn't MATTER which student I pick...I just need to pick *a* student with that value. So I could go back through the dictionary through each member, find *any* member with that value, append that member to the list, and then remove the member from the other list (if I didn't, I would keep adding the same person to the list over and over). And that would actually work, and if you want to do that, by all means, do it. But there are two issues: 1) Your program would actually run pretty slowly if you had, like, 1000000 names in it. It would require nested for loops. 2) Your code would be needlessly complicated to read. The Python motto is if you can use Python methods to make your code short and sweet, do so.

OpenStudy (anonymous):

hmm yeah your right it makes sense alright ill give it your way a try and do it cant you just give me the answerr i hate doing this lol (N)

OpenStudy (anonymous):

ok nvm its straight forward LOL

OpenStudy (anonymous):

Good, because I wasn't going to give you the answer. :)

OpenStudy (anonymous):

wow wth ok question i feel so dumb right now

OpenStudy (anonymous):

say d= {'john':5, 'albert':2, 'tim':9} and i just did sorted(d) which i did i got the answer i was looking for ['albert', 'john', 'tim'] ???

OpenStudy (anonymous):

ok nvm thats how i just introduced my d ok its wrong

OpenStudy (anonymous):

Yeah, the fact that you got a sorted list is complete coincidence, since value sorting in that specific case is exactly the same as alphabetical sorting.

OpenStudy (anonymous):

could you give me another hint i tried sorted(d, key=len) that didnt work then i tried sorted(d, d.get()) and it keeps giving errors

OpenStudy (anonymous):

Well, key=len doesn't work, because you don't care about the length, you care about the value of the key. Your second try is much, much closer. That's the right function. Two things: You forgot to include key=. Also, you leave the parentheses off of the function. It's weird, but it's required.

OpenStudy (anonymous):

The reason you need "key=" is because it's an optional argument, and you need to let Python know that you're using it.

OpenStudy (anonymous):

oo sorted(d, key=d.get)

OpenStudy (anonymous):

okay i have another question that i will post

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!