我想在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
如果您想使用带标签的行和列,请查看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