- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个这样的数组:
elements=[['1', '1', '2'], ['2', '2', '3'], ['3', '3', '4'], ['4', '4', '5'], ['5', '5', '6'], ['6', '6', '7'], ['7', '7', '8'], ['8', '8', '9'], ['9', '9', '10'], ['10', '10', '11'], ['11', '11', '12'], ['12', '12', '13'], ['13', '13', '14'], ['14', '14', '15'], ['15', '15', '16'], ['16', '16', '17'], ['17', '17', '18'], ['18', '18', '19'], ['19', '19', '20'], ['20', '20', '21'], ['21', '21', '22'], ['22', '22', '23'], ['23', '23', '24'], ['24', '24', '25'], ['25', '25', '26'], ['26', '26', '27'], ['27', '27', '28'], ['28', '28', '29'], ['29', '29', '30'], ['30', '30', '31'], ['31', '32', '33'], ['32', '33', '34'], ['33', '34', '35'], ['34', '35', '36'], ['35', '36', '37'], ['36', '37', '38'], ['37', '38', '39'], ['38', '39', '40'], ['39', '40', '41'], ['40', '41', '42'], ['41', '42', '43'], ['42', '43', '44'], ['43', '44', '45'], ['44', '45', '46'], ['45', '46', '47'], ['46', '47', '48'], ['47', '48', '49'], ['48', '49', '50'], ['49', '50', '51'], ['50', '51', '52'], ['51', '52', '53'], ['52', '53', '54'], ['53', '54', '55'], ['54', '55', '56'], ['55', '56', '57'], ['56', '57', '58'], ['57', '58', '59'], ['58', '59', '60'], ['59', '60', '61'], ['60', '61', '62'], ['61', '63', '64'], ['62', '64', '65'], ['63', '65', '66'], ['64', '66', '67'], ['65', '67', '68'], ['66', '68', '69'], ['67', '69', '70'], ['68', '70', '71'], ['69', '71', '72'], ['70', '72', '73'], ['71', '73', '74'], ['72', '74', '75'], ['73', '75', '76'], ['74', '76', '77'], ['75', '77', '78'], ['76', '78', '79'], ['77', '79', '80'], ['78', '80', '81'], ['79', '81', '82'], ['80', '82', '83'], ['81', '83', '84'], ['82', '84', '85'], ['83', '85', '86'], ['84', '86', '87'], ['85', '87', '88'], ['86', '88', '89'], ['87', '89', '90'], ['88', '90', '91'], ['89', '91', '92'], ['90', '92', '93'], ['91', '94', '95'], ['92', '95', '96'], ['93', '96', '97'], ['94', '97', '98'], ['95', '98', '99'], ['96', '99', '100'], ['97', '100', '101'], ['98', '101', '102'], ['99', '102', '103'], ['100', '103', '104'], ['101', '104', '105'], ['102', '105', '106'], ['103', '106', '107'], ['104', '107', '108'], ['105', '108', '109'], ['106', '109', '110'], ['107', '110', '111'], ['108', '111', '112'], ['109', '112', '113'], ['110', '113', '114'], ['111', '114', '115'], ['112', '115', '116'], ['113', '116', '117'], ['114', '117', '118'], ['115', '118', '119'], ['116', '119', '120'], ['117', '120', '121'], ['118', '121', '122'], ['119', '122', '123'], ['120', '123', '124'], ['121', '125', '126'], ['122', '126', '127'], ['123', '127', '128'], ['124', '128', '129'], ['125', '129', '130'], ['126', '130', '131'], ['127', '131', '132'], ['128', '132', '133'], ['129', '133', '134'], ['130', '134', '135'], ['131', '135', '136'], ['132', '136', '137'], ['133', '137', '138'], ['134', '138', '139'], ['135', '139', '140'], ['136', '141', '142'], ['137', '142', '143'], ['138', '143', '144'], ['139', '144', '145'], ['140', '145', '146'], ['141', '146', '147'], ['142', '147', '148'], ['143', '148', '149'], ['144', '149', '150'], ['145', '150', '151'], ['146', '151', '152'], ['147', '152', '153'], ['148', '153', '154'], ['149', '154', '155'], ['150', '155', '156'], ['151', '157', '158'], ['152', '158', '159'], ['153', '159', '160'], ['154', '160', '161'], ['155', '161', '162'], ['156', '162', '163'], ['157', '163', '164'], ['158', '164', '165'], ['159', '165', '166'], ['160', '166', '167'], ['161', '167', '168'], ['162', '168', '169'], ['163', '169', '170'], ['164', '170', '171'], ['165', '171', '172'], ['166', '172', '173'], ['167', '173', '174'], ['168', '174', '175'], ['169', '175', '176'], ['170', '176', '177'], ['171', '177', '178'], ['172', '178', '179'], ['173', '179', '180'], ['174', '180', '181'], ['175', '181', '182'], ['176', '182', '183'], ['177', '183', '184'], ['178', '184', '185'], ['179', '185', '186'], ['180', '186', '187'], ['181', '188', '189'], ['182', '189', '190'], ['183', '190', '191'], ['184', '191', '192'], ['185', '192', '193'], ['186', '193', '194'], ['187', '194', '195'], ['188', '195', '196'], ['189', '196', '197'], ['190', '197', '198'], ['191', '198', '199'], ['192', '199', '200'], ['193', '200', '201'], ['194', '201', '202'], ['195', '202', '203'], ['196', '203', '204'], ['197', '204', '205'], ['198', '205', '206'], ['199', '206', '207'], ['200', '207', '208'], ['201', '208', '209'], ['202', '209', '210'], ['203', '210', '211'], ['204', '211', '212'], ['205', '212', '213'], ['206', '213', '214'], ['207', '214', '215'], ['208', '215', '216'], ['209', '216', '217'], ['210', '217', '218'], ['211', '219', '220'], ['212', '220', '221'], ['213', '221', '222'], ['214', '222', '223'], ['215', '223', '224'], ['216', '224', '225'], ['217', '225', '226'], ['218', '226', '227'], ['219', '227', '228'], ['220', '228', '229'], ['221', '229', '230'], ['222', '230', '231'], ['223', '231', '232'], ['224', '232', '233'], ['225', '233', '234'], ['226', '235', '236'], ['227', '236', '237'], ['228', '237', '238'], ['229', '238', '239'], ['230', '239', '240'], ['231', '240', '241'], ['232', '241', '242'], ['233', '242', '243'], ['234', '243', '244'], ['235', '244', '245'], ['236', '245', '246'], ['237', '246', '247'], ['238', '247', '248'], ['239', '248', '249'], ['240', '249', '250'], ['241', '251', '252'], ['242', '252', '253'], ['243', '253', '254'], ['244', '254', '255'], ['245', '255', '256'], ['246', '256', '257'], ['247', '257', '258'], ['248', '258', '259'], ['249', '259', '260'], ['250', '260', '261'], ['251', '261', '262'], ['252', '262', '263'], ['253', '263', '264'], ['254', '264', '265'], ['255', '265', '266'], ['256', '267', '268'], ['257', '268', '269'], ['258', '269', '270'], ['259', '270', '271'], ['260', '271', '272'], ['261', '272', '273'], ['262', '273', '274'], ['263', '274', '275'], ['264', '275', '276'], ['265', '276', '277'], ['266', '277', '278'], ['267', '278', '279'], ['268', '279', '280'], ['269', '280', '281'], ['270', '281', '282'], ['271', '283', '284'], ['272', '284', '285'], ['273', '285', '286'], ['274', '286', '287'], ['275', '287', '288'], ['276', '288', '289'], ['277', '289', '290'], ['278', '290', '291'], ['279', '291', '292'], ['280', '292', '293'], ['281', '293', '294'], ['282', '294', '295'], ['283', '295', '296'], ['284', '296', '297'], ['285', '297', '298'], ['286', '298', '299'], ['287', '299', '300'], ['288', '300', '301'], ['289', '301', '302'], ['290', '302', '303'], ['291', '303', '304'], ['292', '304', '305'], ['293', '305', '306'], ['294', '306', '307'], ['295', '307', '308'], ['296', '308', '309'], ['297', '309', '310'], ['298', '310', '311'], ['299', '311', '312'], ['300', '312', '313'], ['301', '314', '315'], ['302', '315', '316'], ['303', '316', '317'], ['304', '317', '318'], ['305', '318', '319'], ['306', '319', '320'], ['307', '320', '321'], ['308', '321', '322'], ['309', '322', '323'], ['310', '323', '324'], ['311', '324', '325'], ['312', '325', '326'], ['313', '326', '327'], ['314', '327', '328'], ['315', '328', '329'], ['316', '329', '330'], ['317', '330', '331'], ['318', '331', '332'], ['319', '332', '333'], ['320', '333', '334'], ['321', '334', '335'], ['322', '335', '336'], ['323', '336', '337'], ['324', '337', '338'], ['325', '338', '339'], ['326', '339', '340'], ['327', '340', '341'], ['328', '341', '342'], ['329', '342', '343'], ['330', '343', '344'], ['331', '345', '346'], ['332', '346', '347'], ['333', '347', '348'], ['334', '348', '349'], ['335', '349', '350'], ['336', '350', '351'], ['337', '351', '352'], ['338', '352', '353'], ['339', '353', '354'], ['340', '354', '355'], ['341', '355', '356'], ['342', '356', '357'], ['343', '357', '358'], ['344', '358', '359'], ['345', '359', '360'], ['346', '361', '362'], ['347', '362', '363'], ['348', '363', '364'], ['349', '364', '365'], ['350', '365', '366'], ['351', '366', '367'], ['352', '367', '368'], ['353', '368', '369'], ['354', '369', '370'], ['355', '370', '371'], ['356', '371', '372'], ['357', '372', '373'], ['358', '373', '374'], ['359', '374', '375'], ['360', '375', '376']]
我想知道这个数组第一列的最大值。但是我发现这有问题,因为我不习惯 python。
我试过:
import numpy as np
a = np.array(elements)
numEL = a[np.argmax(a)][0]
但是我得到了错误的结果。我知道它是 360 但它返回 285...
有什么想法吗?
最佳答案
您的元素是字符串,因此比较是字典顺序的。您想使用整数:
>>> elements=[['1', '1', '2'], ['99', '2', '3'],['360', '10', '11']]
>>> a = np.array(elements,dtype=int)
>>> a
array([[ 1, 1, 2],
[ 99, 2, 3],
[360, 10, 11]])
>>> a.max(axis=0)
array([360, 10, 11])
>>> a.max(axis=0)[0]
360
或者只是
>>> a[:,0]
array([ 1, 99, 360])
>>> a[:,0].max()
360
[注意:这是让它工作的方法。至于为什么代码首先返回一个奇怪的答案,@mgilson 在对此答案的评论中解释说。]
关于python - 第一列的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15508460/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!