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

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

修改代碼以處理一長(zhǎng)串字符串

修改代碼以處理一長(zhǎng)串字符串

開(kāi)滿天機(jī) 2022-10-11 21:19:02
我想為一項(xiàng)任務(wù)準(zhǔn)備一長(zhǎng)串?dāng)?shù)據(jù)。我已經(jīng)能夠?qū)⒃趩蝹€(gè)實(shí)例上完成任務(wù)的代碼放在一起,但現(xiàn)在我想讓它通過(guò)一個(gè)列表運(yùn)行。以下是我嘗試過(guò)的。用于測(cè)試的單個(gè)實(shí)例......sentences = ['if the stimulus bill had become hamstrung by a filibuster threat or recalcitrant conservadems']antecedents = ['bill had become hamstrung by']實(shí)際用例是 pandas 數(shù)據(jù)框中的兩列,我已將其轉(zhuǎn)換為列表f = tra_df['sentence'].tolist()b = tra_df['antecedent'].tolist()單個(gè)用例的代碼....results =[]ous = 1ayx = ' '.join([str(elem) for elem in antecedents])ayxx = ayx.split(" ")antlabels = []    for i in range(len(ayxx)):    antlabels.append(ous)    lab = ' '.join([str(elem) for elem in antlabels])     # Build the regex string requiredrx = '({})'.format('|'.join(re.escape(el) for el in antecedents))     # Generator to yield replaced sentencesit = (re.sub(rx, lab, sentence) for sentence in sentences)     # Build list of paired new sentences and old to filter out where not the sameresults = ([new_sentence for old_sentence, new_sentence in zip(sentences, it) if old_sentence != new_sentence])# replace other non 1 values with 0nw_results = ' '.join([str(elem) for elem in results])ew_results= nw_results.split(" ")new_results = ['0' if i is not '1' else i for i in ew_results]labels =([int(e) for e in new_results]) labels這就是我得到的結(jié)果[0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0]大列表的稍微修改的代碼for sentences, antecedents in zip(f, b):    gobels = []    #def format_labels(antecedents,sentences):    results =[]    #lab =[]    ous = 1    ayx = ' '.join([str(elem) for elem in antecedents])    ayxx = ayx.split(" ")    antlabels = []        for i in range(len(ayxx)):        antlabels.append(ous)        lab = ' '.join([str(elem) for elem in antlabels])現(xiàn)在,我得到一個(gè)只有 1 的長(zhǎng)列表,而不是包含 0 和 1 的字符串列表......有什么問(wèn)題?
查看完整描述

1 回答

?
牧羊人nacy

TA貢獻(xiàn)1862條經(jīng)驗(yàn) 獲得超7個(gè)贊

像這樣的東西可能會(huì)更好地?cái)U(kuò)展??赡苓€有一種更 Pythonic 的方式來(lái)執(zhí)行此操作。


a = '1 2 3 4 5'

b = '3 4 6'


a = a.split()

b = b.split()


for idx, val in enumerate(b):

    try:

        a[a.index(val)] = True

    except ValueError:

        pass


for idx, val in enumerate(a):

    if val is not True:

        a[idx] = False


print([1.0 if i else 0.0 for i in a])

# [0.0, 0.0, 1.0, 1.0, 0.0]


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

添加回答

舉報(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)