Matrix中的给定参数的“查找”函数

问题描述 投票:-1回答:1

我想在python中构建一个函数,它能够在自定义矩阵中查找特定值。像这样的东西:

PR1 = [["a","b","c","d"],["LTV1",2,3,4],["LTV2",6,7,8]]
PR2 = [["a","b","c","d"],["LTV1",10,11,12],["LTV2",14,15,16]]
PR3 = [["a","b","c","d"],["LTV1",18,19,20],["LTV2",22,23,24]]

def lookup(....)

lookup("PR1","a","LTV1")=2
lookup("PR2","c","LTV2")=15

你会如何构建这样的“查找”功能?

谢谢,

KS

python lookup
1个回答
0
投票

如果您想使用带标签的行和列,请查看pandas库。

>>> import pandas as pd
>>> 
>>> PR2 = [["a","b","c","d"],["LTV1",10,11,12,13],["LTV2",14,15,16,17]]
>>> columns = PR2[0]
>>> rows = [sub[1:] for sub in PR2[1:]]
>>> index = [sub[0] for sub in PR2[1:]]
>>> 
>>> df = pd.DataFrame(rows, columns=columns, index=index)
>>> df
>>> 
       a   b   c   d
LTV1  10  11  12  13
LTV2  14  15  16  17
>>> df['c']['LTV2']
>>> 16
© www.soinside.com 2019 - 2024. All rights reserved.