找出python中子列表的最大值索引

时间:2017-05-18 16:22:44

标签: python list max sublist

我有n个子列表的列表x。我想制作长度为new_list的{​​{1}},其中包含每个子列表的最大索引。我该怎么办呢?

例如:

x = [[1,4,2,3,5],[3,5,2,1,3],[5,2,1,4,3],[1,2,3,5,4],[4,1,5,2,3]]

(为了便于使用,我在每个子列表中采用了相同的最大值)

因此输出必须是:

new_list = [4,1,0,3,2]

4 个答案:

答案 0 :(得分:1)

如果每个子列表中没有重复项,请尝试以下操作:

new_list = [sub.index(max(sub)) for sub in x]
>>> x = [[1,4,2,3,5],[3,5,2,1,3],[5,2,1,4,3],[1,2,3,5,4],[4,1,5,2,3]]
>>> new_list = [sub.index(max(sub)) for sub in x]
>>> new_list
[4, 1, 0, 3, 2]
>>> 

答案 1 :(得分:1)

您可以使用lambda表达式并映射到l,以获取x中最大元素的索引

l=[[1,4,2,3,5],[3,5,2,1,3],[5,2,1,4,3],[1,2,3,5,4],[4,1,5,2,3]]
print map(lambda x:x.index(max(x)),l)

输出:

[4, 1, 0, 3, 2]

如果你正在使用numpy数组,

>>> print map(lambda x:int(np.where(x==max(x))[0]),l)
[4, 1, 0, 3, 2]

答案 2 :(得分:1)

如果您可以访问<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" type="text/css" href="//fonts.googleapis.com/css?family=Open+Sans" /> <link rel="stylesheet" href="../test/css/index.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div id="myCarousel" class="carousel slide" data-ride="carousel"> <ol class="carousel-indicators"> <li data-target="#myCarousel" data-slide-to="0" class="active"></li> <li data-target="#myCarousel" data-slide-to="1"></li> <li data-target="#myCarousel" data-slide-to="2"></li> </ol> <!-- Wrapper for slides --> <div class="carousel-inner"> <div class="item active"> <img src="../test/images/slider1.png" alt="Los Angeles" style=" height:100vh;width:100vw;"> </div> <div class="item"> <img src="../test/images/slider-2.png" alt="Chicago" style=" height:100vh;width:100vw;"> </div> <div class="item"> <img src="../test/images/slider-3.png" alt="New york" style=" height:100vh;width:100vw;"> </div> </div> </div> </div> <div id="q-nav"> <ul> <li><a class="active q-nav-about" href="#about-us">ABOUT US</a> </li> <li><a class="q-nav-work" href="#work">WORK</a> </li> <li><a class="q-nav-contact" href="#contact-us">CONTACT US</a> </li> </li> </ul> </div> <div class="q-intro-text"> <div class="q-mobile-app-text">MOBILE APP TEXT<br/>All the work and no play makes jack a dull boy! <br/> Join Libr8 cricket league. Unleash the <br/> sportsman in you</div> </div> <div id="#about-us"> <img src="../test/images/slider-3.png" alt="New york" style=" height:100vh;width:100vw;"> </div> <div id="#work"> <img src="../test/images/slider-2.png" alt="Chicago" style=" height:100vh;width:100vw;"> </div> <div id="#contact-us"> <img src="../test/images/slider1.png" alt="Los Angeles" style=" height:100vh;width:100vw;"> </div> <script src="../test/scripts/scrolling.js"></script> </body> </html>,则非常简单:

numpy

答案 3 :(得分:-1)

通过循环并获取每个子列表的最大值来创建一个新列表:

像这样:

x = [[1,4,2,3,5],[3,5,2,1,3],[5,2,1,4,3],[1,2,3,5,4],[4,1,5,2,3]]

max_index = [i.index(max(i)) for i in x]