Python 寻找产生5个大和的指数

Python 寻找产生5个大和的指数,python,Python,假设我有两个列表: a = [0.1,0.2,0.4,0.5] b = [0.5,0.6,0.7, 0.8] c = [all the sums of a and b] c = [] for a_ in a: for b_ in b: c.append(a_+b_) 现在,c将包含a和b之和的值_ 获取c中最大的5个元素 然后返回a和b指数产生的最大总和 我的想法是跟踪像c[index_of a][index_of b]=a_+b_ 所以,问题是a和b的什么指数返回k最大

假设我有两个列表:

a = [0.1,0.2,0.4,0.5]
b = [0.5,0.6,0.7, 0.8]

c = [all the sums of a and b]

c = []
for a_ in a:
   for b_ in b:
      c.append(a_+b_)
现在,c将包含a和b之和的值_ 获取c中最大的5个元素

然后返回a和b指数产生的最大总和

我的想法是跟踪像
c[index_of a][index_of b]=a_+b_

所以,问题是a和b的什么指数返回k最大的a+b

然后搜索最大的a+b_ 但我想知道是否有更好的方法来做到这一点。 谢谢

编辑:以上问题的答案是:

c = [0.6, 0.7, 0.7999999999999999, 0.9, 0.7, 0.8, 0.8999999999999999, 1.0, 0.9, 1.0, 1.1, 1.2000000000000002, 1.0, 1.1, 1.2, 1.3]
5
maximum=已排序(c,reverse=True)[:5]

largest = [1.3, 1.2000000000000002, 1.2, 1.1, 1.1]
And then corresponding indices in a and b list are:
a_index = 3, b_index = 3 (1.3)
a_index = 2, b_index = 3 (1.2)
等等

一种暴力手段

largest, a_index, b_index = [0] * 3
for i, n1 in enumerate(a):
    for j, n2 in enumerate(b):
        if n1 + n2 > largest:
            largest = n1 + n2
            a_index = i
            b_index = j
a=[0.1,0.2,0.4,0.5]
b=[0.5,0.6,0.7,0.8]
#找出所有可能的和
#-与使用的项目索引的总和一起存储
#-enumerate将为我们提供索引和项目
c=[(x+y,x_i,y_i)表示枚举(a)中的x_i,x
对于y_i,y在枚举中(b)]
#将此列表排序
#-当我们对元组进行排序时,它将在第一个元组上进行排序
#首先是元组项(即总和),然后是其他项
#-那么就拿清单上的五个项目
索引=[(x_i,y_i)用于排序(c)[-5:]

使用
枚举
,可以保留每个元素的原始索引。然后需要指定键作为求和的结果

下面将生成一个元组列表,其中包含a的索引、b的索引以及按该顺序排列的和

sorted([(x[0], y[0], x[1]+y[1]) for x in enumerate(a)
                                for y in enumerate(b)],
       key=lambda x: x[2], reverse=True)

基于@donkopotamus的代码,但如果输入列表
a
b
足够大,则需要的内存和CPU时间应该少得多。它只是使用生成器表达式,出于以下目的:

import heapq

a = [0.1,0.2,0.4,0.5]
b = [0.5,0.6,0.7, 0.8]

# Change: generator expression for all of the possible sums (not realized yet)
# - store with the sum the item indices that were used
# - enumerate will give us the index and the item
c = ((x + y, x_i, y_i) for x_i, x in enumerate(a) 
                       for y_i, y in enumerate(b))

# Use heapq.nlargest to get the indices that produce the largest sums
# without storing all sums in memory (it maintains a heap internally that
# contains only the five largest values seen to date at once)
# - as we are sorting tuples, it will sort on the first 
#   tuple item initially (ie the sum), and then the others
# - then just take the list five items
indices = [(x_i, y_i) for _, x_i, y_i in heapq.nlargest(5, c)]

问题中所示案例的预期输出是什么?如果你解释了你在做什么以及为什么它有用,我会支持这个答案。在幕后,
heapq
只是从集合中排序(维护一个堆)。如果你把整个系列都弄脏了,把它弄脏了。。。这与排序的运行时/空间复杂性(O(n*log(n))相同。如果您用五项建立堆(任意获取前五项),然后在其余项上使用
heappushpop()
方法,那么您就是在恒定的空间和线性时间内求解它。@JimDennis:
heapq.nlargest
维护请求大小的堆,因此,在任何给定的时间内,只有五个元素被存储,当较大的元素进入时,较小的元素被逐出并释放。
heappushpop
的复杂度是
O(log(n))
,但在这种情况下
n
5
,而不是函数输入的总数。因此,总体成本与
log(5)*len(输入)
成比例,而不是
log(len(输入))*len(输入)
,这就是排序成本。@shaderanger:Hmmm。我不知道nlargest()会以这种方式工作。我从文档中假设有必要在heapify()之后的列表中调用它(整个列表)。(无论如何,我的用例一直都是保持连续流的top-K。)
heapq
模块有点奇怪。它有“低级”堆操作函数,加上一系列从堆原语构建的实用函数。很多人都犯同样的错误。我会注意到,如果内存不是问题,
sorted
无论如何都可以获胜,这仅仅是因为
sorted
是用C实现的,而
heapq
只用C实现原语;实用程序函数是用Python编写的,因此它们会产生大量开销,而
sorted
可以避免这些开销。即便如此,对于10000个随机元素的测试用例,在我的Py 3.5安装上选择5个最大的,
sorted
nlargest
@shadowranger花费了约6倍的时间,我考虑了heapq替代方案,但正如您所注意到的,
sorted
比小列表快得多。但是当我考虑
heapq
时,我考虑了分别获得每个列表
a
b
的前5名成员的可能性,并利用我们知道最大元素必须由
a
b
等的最大值组成的事实。