python create dict using list of strings with length of strings as values
问题 I'm sure this can be done, but I have thus far been unsuccessful: I have a list of strings. I want to create a dictionary with the length of said strings (which can be expressed as a range) as the key and the string itself as the value. example: Here's something like the list I have: ['foo','bar','help','this','guy'] I'd like to end up with a dictionary like this: {3:['foo','bar','guy], 4:['this','help']} 回答1: You can use a dictionary as a container with setdefault : lst = ['foo','bar','help'