Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 获取字典中的最大值和最小值_Python_Dictionary - Fatal编程技术网

Python 获取字典中的最大值和最小值

Python 获取字典中的最大值和最小值,python,dictionary,Python,Dictionary,那些在我的字典里是d。 如何获取字典中每个键的最大值和最小值,并获取值所在的索引 例如: 在伊利诺伊州,26是指数5的最大值,3是指数15的最小值。 在印第安纳州:13是指数7的最大值,2是指数14的最小值 输出: Illinois: ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'] Indiana: ['7', '6', '7', '8', '11', '11', '

那些在我的字典里是d。 如何获取字典中每个键的最大值和最小值,并获取值所在的索引

例如: 在伊利诺伊州,26是指数5的最大值,3是指数15的最小值。 在印第安纳州:13是指数7的最大值,2是指数14的最小值

输出:

Illinois: ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3']
Indiana: ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']
我该怎么做

Illinois: 26 in index 5 and 3 in index 15
Indiana: 13 in index 7 and 2 in index 14

您可以打印最大和最小值,因为您的字符串如下所示:

(假设您只需要第一次出现)


有点纠结,但似乎做的工作

 MY_D = {'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'],
'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']}

for k,v in MY_D.items():
     #This assumes that everything in v is an int, or rather can be converted to one.
     my_l = [int(n) for n in v]
     #if not
     #my_l = [int(n) for n in v if n.isdigit()]
     _max, _min = max(my_l), min(my_l)
     print("%s: Min - %d in index %d, Max - %d in index %d" % (k, _min,  my_l.index(_min), _max, my_l.index(_max)))
输出:

d = {"Illinois": ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'],
"Indiana": ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']}

if __name__ == "__main__":
    print d

    for state in d:
        # returns the numbers with their index (#, index)
        pairs = [(int(d[state][x]), x) for x in xrange(len(d[state]))]
        minpair = min(pairs)
        maxpair = max(pairs)
        print "%s: %d in index %d and %d in index %d"%(state,maxpair[0],maxpair[1],
        minpair[0],minpair[1])
<> P>绕过空白字符串,可以将列表理解分解为

{'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2'], 'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3']}
Indiana: 13 in index 6 and 2 in index 13
Illinois: 26 in index 4 and 3 in index 14

下面是一个返回dict
{country:(maxval,index),(minval,index))}


你能展示你自己的代码尝试吗?解释你目前遇到的困难。最后,请显示一个添加的@idjaw,不知道我将如何对其进行编码………从字面上看,此
max(list)
用于列表中的max,而
list.index(max[list])
用于索引位置。@Adib:
list.index(max(list))
不会找到所有出现的情况。它将只找到第一个。@zondo这是否足够,因为您只需要最大值(并且只返回一个索引)?
country!=将
状态作为次要编辑。还有,这也不行。谢谢。我还更新了代码,以获得
O(n)
复杂性,以及更可读的索引访问。无法工作为什么?忘记了
int
转换。现在已修复。
max
for strings比较
ord
值我有一个问题,假设我将伊利诺伊州的第一个索引替换为空字符串“”,我该如何解决这个问题?我有一个问题,假设我将伊利诺伊州的第一个索引替换为空字符串“”,我该怎么处理呢?你应该写一个案例来处理这个异常
[int(n)表示n in v if n]
或者更好的
[int(n)表示n in v if n.isdigit()]
谢谢!这很有帮助!次要注意事项-在迭代
项时,您不需要强制转换为列表。我有一个问题,假设我将伊利诺伊州的第一个索引替换为空字符串“”,我将如何解决此问题?修复了回答注释的主要答案,因为这里没有格式化。
{'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2'], 'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3']}
Indiana: 13 in index 6 and 2 in index 13
Illinois: 26 in index 4 and 3 in index 14
    pairs = []
    for x in xrange(len(d[state])):
        try:
            pairs.append( (int(d[state][x]), x) )
        except ValueError:
            pass # not a valid number
d = {
    'Illinois': ['13', '12', '18', '23', '26', '25', '24', '19', '13', '10', '15', '14', '14', '4', '3'],
    'Indiana': ['7', '6', '7', '8', '11', '11', '13', '12', '7', '7', '7', '7', '9', '2', '2']
}


maxmin = {}
for state, numbers in d.items():
    maxmin[state] = (
        max(enumerate(numbers), key=lambda x: int(x[1])),
        min(enumerate(numbers), key=lambda x: int(x[1]))
    )


print(maxmin)