Python miminum value in dictionary of lists

前端 未结 3 958
情书的邮戳
情书的邮戳 2021-01-21 05:41

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

3条回答
  •  忘掉有多难
    2021-01-21 06:27

    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.

提交回复
热议问题