2

So I found that to get the maximum dictionary value in I can do:

dict[max(dict, key=dict.get)]

My dictionary looks like this: {article_title:number of how many different articles link to it}

example: {'10th century': 2, 'Poland' : 0, 'Mexico' : 11}

From group() I get a list of tuples (article_title, article object), example: [(Mexico, Mexico()), (Poland, Poland())]

For that group, I want to check what is the max value article_title may have.

But how do I find dictionary value of a specific group of keys? I'm really lost... I think what I wrote doesn't make sense:

dict = my dictionary
group_of_keys = group()  # returns specific list of tuples. The first term of the tuple is my key, the second is irrelevant
max_value = dict[max(dict[group_of_keys], key=dict.get)]

Help please!

Community
  • 1
  • 1
Bun
  • 51
  • 5
  • 4
    Can you provide the definition of `group()`, as well as an example of the dictionary and your expected outputs please. – Tagc Jan 17 '17 at 15:56
  • 1
    `dict = my dictionary` shadows the `dict` keyword, or at least it would do if it didn't contain whitespace and therefore raise a `SyntaxError` – Chris_Rands Jan 17 '17 at 16:01
  • I added an example of what the dictionary and group() output should look like. – Bun Jan 17 '17 at 16:26
  • @Bun if you convert your group to just having the keys of the dictionary , I.e. 'poland', 'mexico', then I think my answer works – algrebe Jan 17 '17 at 16:29

2 Answers2

3

I am assuming group() returns a list of keys. If so you can get the values of those keys and then find the max out of them.

max(map(dict.get, groups()))

EDIT: As you clarified that group() returns a tuple of (article_title, article_object) and you want article_title as key what we can do is get the values of those keys as dict.get(title) for title, article in group() and then find the max among those values. So, the answer to your question is:

max(dict.get(title) for title, article in group())

Small note: dict is not a good name for a variable since it shadows python's reserved keyword dict.

emre.
  • 1,296
  • 11
  • 13
1

By group I assume you mean a subset of your keys

my_dict = {} # your dictionary
group = get_group(my_dict)
# since your group returns tuples of (key, something else)
group = [ i[0] for i in group ]
max_group_key = max(group, my_dict.get)
max_group_value = my_dict[max_group_key]

Just for clarity, I'm taking a dictionary of month to temperatures. max_key would tell me the month that had the highest temperature. max_group_key would tell me the month within that group that had the highest temperature.

temperature = {
    "jan": 17, "feb": 18, "mar": 19, "apr": 24, "may": 26, 
    "jun": 25, "jul": 22, "aug": 21, "sep": 20, "oct": 20,
    "nov": 18, "dec": 15
}

# hottest month
max_key = max(temperature, key=temperature.get)
max_val = temperature[max_key]

print("hottest month: {0}, temperature: {1}".format(max_key, max_val))

# only for a few months
group = [ ("jan", "foo"), ("feb", "bar"), ("jun", "baz") ]
group = [ i[0] for i in group ]
max_group_key = max(group, key=temperature.get)
max_group_val = temperature[max_group_key]

print("hottest month: {0}, temperature: {1}".format(max_group_key, max_group_val))
algrebe
  • 1,621
  • 11
  • 17