Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.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
如何理解pythonmax函数中的键_Python_Numpy - Fatal编程技术网

如何理解pythonmax函数中的键

如何理解pythonmax函数中的键,python,numpy,Python,Numpy,我有一个示范代码: import numpy as np x = [np.array(range(10))] res = max(x, key=np.size) 我预计res将是10,但事实上 >>> res array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) 任何人都可以解释这一点吗?您的第一个问题是您使用的是范围(10),该范围增加到10,但不包括数字10。这里的方法是x=[np.array(range(11))] 为了最大限度地利用这一点,使用

我有一个示范代码:

import numpy as np
x = [np.array(range(10))]
res = max(x, key=np.size)
我预计
res
将是
10
,但事实上

>>> res
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

任何人都可以解释这一点吗?

您的第一个问题是您使用的是范围(10),该范围增加到10,但不包括数字10。这里的方法是
x=[np.array(range(11))]

为了最大限度地利用这一点,使用numpy文档,您可以使用np.amax,可以这样做:
res=np.amax(x)
,它应该可以工作


-文档链接。

您的代码存在一些问题。 将NumPy数组初始化到Python列表中

变量
x
应该是
np.array(范围(10))
而不是
[np.array(范围(10))]

现在
x
如下所示:
数组([0,1,2,3,4,5,6,7,8,9])

在这种情况下,
max()
函数不需要
np.size
,因为函数默认搜索最大值。在本例中,不需要
。如果您使用的是词典,则需要使用key。例如:

x = {'first': 1, 'second': 5, 'third': 2}
In [109]: L = [str(i) for i in range(20)]                                                                                                                                                                                                                                     

In [110]: L                                                                                                                                                                                                                                                                   
Out[110]: 
['0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 '10',
 '11',
 '12',
 '13',
 '14',
 '15',
 '16',
 '17',
 '18',
 '19']

In [111]: max(L)                                                                                                                                                                                                                                                              
Out[111]: '9'  # '9' is the lexicographically max string

In [112]: max(L, key=int)                                                                                                                                                                                                                                                     
Out[112]: '19'  # 19 is the numerically max number, int('19') has this value
如果希望
max()
返回具有最长键的键/值,可以将其用作:

max(x.items(), key=len)
或者,如果您想要最高的值:

max(x.items(), key=lambda x: x[1])
使用
,您可以指定希望
max()
如何工作


res
将具有值
9
,因为
范围(10)
的最大值是number
9
。要使NumPy数组具有值
10
,需要使用
range(11)
(函数不包括在内)。

示例代码显示了一个python列表(
x
),其中包含一个包含10个元素的NumPy数组。该列表的最大值(不考虑键)是该numpy数组

键的作用是什么

这是一个函数,用于计算列表中每个元素的值,该值与其表示值不同。此函数计算的值最大的元素就是列表的最大值

例如:

x = {'first': 1, 'second': 5, 'third': 2}
In [109]: L = [str(i) for i in range(20)]                                                                                                                                                                                                                                     

In [110]: L                                                                                                                                                                                                                                                                   
Out[110]: 
['0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 '10',
 '11',
 '12',
 '13',
 '14',
 '15',
 '16',
 '17',
 '18',
 '19']

In [111]: max(L)                                                                                                                                                                                                                                                              
Out[111]: '9'  # '9' is the lexicographically max string

In [112]: max(L, key=int)                                                                                                                                                                                                                                                     
Out[112]: '19'  # 19 is the numerically max number, int('19') has this value
如果不是这样写的话,你的例子会更清楚:

In [106]: L = [np.array(range(i)) for i in range(1, 11)]                                                                                                                                                                                                                      

In [107]: L                                                                                                                                                                                                                                                                   
Out[107]: 
[array([0]),
 array([0, 1]),
 array([0, 1, 2]),
 array([0, 1, 2, 3]),
 array([0, 1, 2, 3, 4]),
 array([0, 1, 2, 3, 4, 5]),
 array([0, 1, 2, 3, 4, 5, 6]),
 array([0, 1, 2, 3, 4, 5, 6, 7]),
 array([0, 1, 2, 3, 4, 5, 6, 7, 8]),
 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])]

In [108]: max(L, key=np.size)                                                                                                                                                                                                                                                 
Out[108]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

No1。额外的
[]

阵列周围有一个额外的
[]
。因此,在代码中,
max
函数实际上只查看包含
np.array
的一个元素的列表。因为只有一个元素,所以它总是相同的

作为测试,在同一终端中,键入
x
,注意它被括号包围

只需从阵列def周围卸下
[]
,如下所示:
x=np.array(范围(10))
。或者你可以修改它,就像下面我留下的例子一样

No2。最大键数 键函数允许您在检查最大值之前将函数传递到值中。例如,如果你有这样的东西:

x = [np.array(range(4), np.array(7)]
res = max(x, key=np.size)

然后
res
将是
数组([0,1,2,3,4,5,6])
。这是因为程序将首先对每个元素(在本例中为两个数组)运行
np.size()
,然后对这些值运行
max
。因此,在我们的示例中,
max
函数实际上会在
[4,7]

上运行。该示例令人困惑。事实上,numpy数组是python列表中唯一一个max元素为的元素computed@inspectorG4dget这也让我感到困惑。:-)max()总是在其输入中输出原始元素
arg1
?您有一个包含单个元素的列表。无论该元素是什么(或键是什么),它始终是该列表的最大值。恰好这个元素是一个numpy数组……我想OP是在要求对键函数的总体工作原理进行澄清。我已经编辑了答案,谢谢您的输入。