4 回答

TA貢獻(xiàn)1790條經(jīng)驗(yàn) 獲得超9個(gè)贊
對(duì)于每個(gè)有趣的函數(shù),您只需遍歷 csv 列表,檢查第一個(gè)元素是否以它開(kāi)頭
csvCpReportContents = [
['[PLT] rand (DEBUG INFO NOT FOUND)', '11', '15'],
['rand', '10', '11', '12'],
[],
['multiply_matrices()', '23', '45']]
fun=['multiply_matrices()','[PLT] rand','rand']
for f in fun:
for c in csvCpReportContents:
if len(c) and c[0].startswith(f):
print(f'fun function {f} is in csv row {c}')
輸出
fun function multiply_matrices() is in csv row ['multiply_matrices()', '23', '45']
fun function [PLT] rand is in csv row ['[PLT] rand (DEBUG INFO NOT FOUND)', '11', '15']
fun function rand is in csv row ['rand', '10', '11', '12']
更新了代碼,因?yàn)槟牧藛?wèn)題中的測(cè)試用例和要求。我的第一個(gè)答案是基于您想要匹配以 item from fun 開(kāi)頭的行的測(cè)試用例?,F(xiàn)在您似乎已經(jīng)更改了該要求以匹配完全匹配,如果不完全匹配匹配,則以匹配開(kāi)頭。下面的代碼已更新以處理該場(chǎng)景。但是我會(huì)說(shuō)下次你的問(wèn)題要清楚,不要在幾個(gè)人回答后改變標(biāo)準(zhǔn)
csvCpReportContents =[
['[PLT] rand (DEBUG INFO NOT FOUND)', '11', '15'],
['rand', '10', '11', '12'],
['__random_r', '23', '45'],
['__random', '10', '11', '12'],
[],
['multiply_matrices()','23','45'] ]
fun = ['multiply_matrices()','__random_r','__random','asd']
for f in fun:
result = []
for c in csvCpReportContents:
if len(c):
if f == c[0]:
result = c
elif not result and c[0].startswith(f):
result = c
if result:
print(f'fun function {f} is in csv row {result}')
else:
print(f'fun function {f} is not vound in csv')
輸出
fun function multiply_matrices() is in csv row ['multiply_matrices()', '23', '45']
fun function __random_r is in csv row ['__random_r', '23', '45']
fun function __random is in csv row ['__random', '10', '11', '12']
fun function asd is not vound in csv

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超8個(gè)贊
具有自定義search_by_func_name功能:
csvCpReportContents = [
['[PLT] rand (DEBUG INFO NOT FOUND)', '11', '15'],
['rand', '10', '11', '12'],
[],
['multiply_matrices()', '23', '45']]
fun = ['multiply_matrices()', '[PLT] rand', 'rand']
def search_by_func_name(name, content_list):
for lst in content_list:
if any(i.startswith(name) for i in lst):
return lst
print(search_by_func_name(fun[1], csvCpReportContents)) # ['[PLT] rand (DEBUG INFO NOT FOUND)', '11', '15']
print(search_by_func_name(fun[2], csvCpReportContents)) # ['rand', '10', '11', '12']

TA貢獻(xiàn)1784條經(jīng)驗(yàn) 獲得超8個(gè)贊
上面的輸入是嵌套列表,所以你必須考慮二維索引,例如 l = [[1,2,3,4],[2,5,7,9]] 來(lái)查找你必須使用的索引的 3 個(gè)數(shù)字元素l[0][2]

TA貢獻(xiàn)1856條經(jīng)驗(yàn) 獲得超17個(gè)贊
您也可以像我在下面的代碼中那樣使用 call_fun 函數(shù)。
def call_fun(fun_name):
for ind,i in enumerate(csvCpReportContents):
if i:
if i[0].startswith(fun_name):
return csvCpReportContents[ind]
# call_fun(fun[2])
# ['rand', '10', '11', '12']
添加回答
舉報(bào)