第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

在列表中搜索確切的字符串

在列表中搜索確切的字符串

小唯快跑啊 2022-06-14 16:27:22
我正在做一個(gè)練習(xí),我需要從列表中搜索確切的函數(shù)名稱fun并從另一個(gè)列表中獲取相應(yīng)的信息detail。這是動(dòng)態(tài)列表detail: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包含要搜索的函數(shù)名稱的列表:fun = ['multiply_matrices()','__random_r','__random']函數(shù)的預(yù)期輸出fun[2] ['__random', '10', '11', '12']函數(shù)的預(yù)期輸出fun[1]['__random_r', '23', '45'],這是我嘗試過(guò)的fun[2]:for i in range(0, len(csvCpReportContents)):    row = csvCpReportContents[i]    if len(row)!=0:        search1 = re.search("\\b" + str(fun[2]).strip() + "\\b", str(row))        if search1:            print(csvCpReportContents[i])請(qǐng)向我建議如何搜索確切的單詞并僅獲取該信息。
查看完整描述

4 回答

?
富國(guó)滬深

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


查看完整回答
反對(duì) 回復(fù) 2022-06-14
?
有只小跳蛙

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']


查看完整回答
反對(duì) 回復(fù) 2022-06-14
?
青春有我

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]



查看完整回答
反對(duì) 回復(fù) 2022-06-14
?
慕慕森

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']


查看完整回答
反對(duì) 回復(fù) 2022-06-14
  • 4 回答
  • 0 關(guān)注
  • 191 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)