4

Sorry about the question repost...I should have just edited this question in the first place. Flagged the new one for the mods. Sorry for the trouble

Had to re-write the question due to changed requirements.

I have a dictionary such as the following:

d = {'a': [4, 2], 'b': [3, 4], 'c': [4, 3], 'd': [4, 3], 'e': [4], 'f': [4], 'g': [4]}

I want to get the keys that are associated with the smallest length in the dictionary d, as well as those that have the maximum value.

In this case, the keys with the smallest length (smallest length of lists in this dictionary) should return

'e, 'f', 'g'

And those with the greatest value(the sum of the integers in each list) should return

'b' 'c'

I have tried

min_value = min(dict.itervalues())
min_keys = [k for k in d if dict[k] == min_value]

But that does not give me the result I want.

Any ideas?

Thanks!

user1530318
  • 25,507
  • 15
  • 37
  • 48

4 Answers4

2

Your problem is that your lists contain strings ('2'), and not integers (2). Leave out the quotes, or use the following:

min_value = min(min(map(int, v) for v in dct.values()))
min_keys = [k for k,v in d.items() if min_value in map(int, v)]

Similarily, to calculate the keys with the max length:

max_length = max(map(len, dct.values()))
maxlen_keys = [k for k,v in d.items() if max_length == len(v)]

Also, it's a bad idea to use dict as a variable name, as doing so overshadows the built-in dict.

phihag
  • 278,196
  • 72
  • 453
  • 469
1

You can use min() with a key= argument, and specify a key function that compares the way you want.

d = {'a': ['1'], 'b': ['1', '2'], 'c': ['2'], 'd':['1']}

min_value = min(d.values())
min_list = [key for key, value in d.items() if value == min_value]
max_len = len(max(d.values(), key=len))
long_list = [key for key, value in d.items() if len(value) == max_len]

print(min_list)
print(long_list)

Notes:

0) Don't use dict as a variable name; that's the name of the class for dictionary, and if you use it as a variable name you "shadow" it. I just used d for the name here.

1) min_value was easy; no need to use a key= function.

2) max_len uses a key= function, len(), to find the longest value.

steveha
  • 74,789
  • 21
  • 92
  • 117
  • for some reason, when I try out the min_value on my dictionary, which has like 20 lists within it that are of the same length, it gives me only one list, which is also the wrong list... – user1530318 Jul 30 '12 at 21:31
  • Please explain what you mean. `min()` will only return one value; that's why I also have `min_list` which collects all the keys that have that value. Also, I just wrote this code to let `min()` do its default comparison. If you want some different comparison, please explain what it is that you want. – steveha Jul 30 '12 at 22:45
1

How about using sorting and lambdas?

#!/usr/bin/env python
d = {'a': ['1'], 'b': ['1', '2'], 'c': ['8', '1'], 'd':['1'], 'e':['1', '2', '3'], 'f': [4, 1]}
sorted_by_sum_d = sorted(d, key=lambda key: sum(list(int(item) for item in d[key])))
sorted_by_length_d = sorted(d, key=lambda key: len(d[key]))
print "Sorted by sum of the items in the list : %s" % sorted_by_sum_d
print "Sorted by length of the items in the list : %s" % sorted_by_length_d

This would output:

Sorted by sum of the items in the list : ['a', 'd', 'b', 'f', 'e', 'c']
Sorted by length of the items in the list : ['a', 'd', 'c', 'b', 'f', 'e']

Be aware I changed the initial 'd' dictionary (just to make sure it was working)

Then, if you want the item with the biggest sum, you get the last element of the sorted_by_sum_d list.

(I'm not too sure this is what you want, though)

Edit:

If you can ensure that the lists are always going to be lists of integers (or numeric types, for that matter, such as long, float...), there's not need to cast strings to integers. The calculation of the sorted_by_sum_d variable can be done simply using:

d = {'a': [1], 'b': [1, 2], 'c': [8, 1], 'd':[1], 'e':[1, 2, 3], 'f': [4, 1]}
sorted_by_sum_d = sorted(d, key=lambda key: sum(d[key]))
Savir
  • 17,568
  • 15
  • 82
  • 136
0

I've found such a simple solution:

min_len = len(min(d.values(), key=(lambda value: len(value))))  # 1
min_keys = [key for i, key in enumerate(d) if len(d[key]) == min_len]  # ['e', 'f', 'g']
maciejwww
  • 1,067
  • 1
  • 13
  • 26