我试图比较两个赌注的名单从两个赌注.它们看起来像这样:

List1 = ['2.66', '3.79', '1.88', '1.61', '2.51', '1.29', '2.29', '2.56', '3.16', '2.05', '2.95', '2.64', '2.26', '3.17', '2.64', '2.25']
List2 = ['2.70', '4.40', '1.87', '1.56', '2.50', '1.26', '2.33', '2.60', '3.20', '2.04', '3.00', '2.65', '2.25', '3.20', '2.65', '2.22']

我需要把它们合并得到最高的赔率.我已经和numpy做过了:

numpy.array([List1, List2]).astype(float).max(axis = 0)
FinalList = [2.7 4.4 1.88 1.61 2.51 1.29 2.33 2.6 3.2 2.05 3.2 2.65 2.26 3.2 2.65 2.25]

问题是我不知道每个索引属于哪个列表.在这个例子中,我需要得到的是:

NamesLists = [List2, List2, List1, List1, List1, List1, List2, List2, List2, List1, List2, List2, List1,  List2, List2, List1]

但我真的不知道该怎么做.

推荐答案

你可以把argmaxtake_along_axis组合起来:

import numpy

List1 = ['2.66', '3.79', '1.88', '1.61', '2.51', '1.29', '2.29', '2.56', '3.16', '2.05', '2.95', '2.64', '2.26', '3.17', '2.64', '2.25']
List2 = ['2.70', '4.40', '1.87', '1.56', '2.50', '1.26', '2.33', '2.60', '3.20', '2.04', '3.00', '2.65', '2.25', '3.20', '2.65', '2.22']

tmp = numpy.array([List1, List2]).astype(float)
idx = tmp.argmax(axis=0)

FinalList = numpy.take_along_axis(tmp, idx[None], axis=0)[0]
# or: FinalList = tmp[idx[None], numpy.arange(tmp.shape[1])][0]
# array([2.7 , 4.4 , 1.88, 1.61, 2.51, 1.29, 2.33, 2.6 , 3.2 , 2.05, 3.  ,
#        2.65, 2.26, 3.2 , 2.65, 2.25])

NamesLists = numpy.array(['List1', 'List2'])[idx]
# array(['List2', 'List2', 'List1', 'List1', 'List1', 'List1', 'List2',
#        'List2', 'List2', 'List1', 'List2', 'List2', 'List1', 'List2',
#        'List2', 'List1'], dtype='<U5')

请注意,idx的形式如下:

array([1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 0, 1, 1, 0])

这可能比['List2', 'List2', 'List1', ...]

Python相关问答推荐

Python中使用时区感知日期时间对象进行时间算术的Incredit

提取两行之间的标题的常规表达

为什么带有dropna=False的groupby会阻止后续的MultiIndex.dropna()工作?

如何获取TFIDF Transformer中的值?

如何过滤包含2个指定子字符串的收件箱列名?

ODE集成中如何终止solve_ivp的无限运行

Pandas DataFrame中行之间的差异

多处理队列在与Forking http.server一起使用时随机跳过项目

调用decorator返回原始函数的输出

在Python中调用变量(特别是Tkinter)

如何防止Pandas将索引标为周期?

为什么调用函数的值和次数不同,递归在代码中是如何工作的?

计算空值

在Docker容器(Alpine)上运行的Python应用程序中读取. accdb数据库

将CSS链接到HTML文件的问题

按条件计算将记录拆分成两条记录

与同步和异步客户端兼容的Python函数

通过对列的其余部分进行采样,在Polars DataFrame中填充_null`?

极点:在固定点扩展窗口

如何将验证器应用于PYDANC2中的EACHY_ITEM?