開(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]
添加回答
舉報(bào)
0/150
提交
取消