试图用更大的字符串中的另一个字符串列表替换字符串列表?

时间:2017-05-10 19:31:20

标签: python

我开始对此代码进行挖掘并希望获得某人的指导。基本上,我想用列表blank_space替换我的字符串driver_knowldege_test中的answer_1。我希望它也按顺序排列(这就是我使用索引的原因),但这也是我发现自己陷入困境的地方。我甚至不确定这些词是否正在相互替换。

driver_knowledge_test = "When you're going to drive it is important to 
always put on your ___1___, including your passengers. \nIf there are no 
lanes marked on the road, you should drive in the ___2___ side of the road.             
\nIt's getting dark and the sun is fading, you should turn on the ___3___. 
\nBefore driving on a freeway, you should make sure you have enough ___4___, 
oil, water and the correct tyre pressure."
answer_1 = ['seatbelts', 'left', 'light', 'fuel']

blank_space = ["___1___", "___2___", "___3___", "___4___"]

def replace_blank_space(blank, sentence, answer):
replaced = []
sentence = sentence.split()
answer = 0
blank_space = 0
for word in sentence:
    replacements = finding_blank(word, list_of_blank)
    if word_to_be_replaced != None:
        word = word.replace(replacements, answer)
        replaced.append(word)
        answer += 1
        blank_space += 1
    else:
        replaced.append(word)
replaced = " ".join(replaced)
return replaced

def finding_blank(word, list_of_blank):
for ea_blank in blank_space:
    if ea_blank in blank_space: #This equals to teh whole list.
        return ea_blank
return None




print replace_blank_space(blank_space, driver_knowledge_test, answer_1)

2 个答案:

答案 0 :(得分:1)

我认为不必split段落或搜索空白或其他任何内容。只需像平常一样致电replace

driver_knowledge_test = "When you're going to drive it is important to \
always put on your ___1___, including your passengers. \nIf there are no \
lanes marked on the road, you should drive in the ___2___ side of the road. \
\nIt's getting dark and the sun is fading, you should turn on the ___3___. \
\nBefore driving on a freeway, you should make sure you have enough ___4___, \
oil, water and the correct tyre pressure."

answer_1 = ['seatbelts', 'left', 'light', 'fuel']
blank_space = ["___1___", "___2___", "___3___", "___4___"]

for space, answer in zip(blank_space, answer_1):
    driver_knowledge_test = driver_knowledge_test.replace(space, answer)

print(driver_knowledge_test)

结果:

When you're going to drive it is important to always put on your seatbelts, including your passengers.
If there are no lanes marked on the road, you should drive in the left side of the road.
It's getting dark and the sun is fading, you should turn on the light.
Before driving on a freeway, you should make sure you have enough fuel, oil, water and the correct tyre pressure.

答案 1 :(得分:0)

基于@ Kevin的答案,除了使用split展示解决方案:

import string
answer_1 = ['seatbelts', 'left', 'light', 'fuel']

blank_space = ["___1___", "___2___", "___3___", "___4___"]

answer = {blank_space[i]:answer_1[i] for i in range(len(answer_1))}

driver_knowledge_test = "When you're going to drive it is important to 
always put on your ___1___, including your passengers. \nIf there are no lanes marked on the road, you should drive in the ___2___ side of the road.\nIt's getting dark and the sun is fading, you should turn on the ___3___. \nBefore driving on a freeway, you should make sure you have enough ___4___, oil, water and the correct tyre pressure."

new_answer = driver_knowledge_test.split()

final_answer = [i.strip(i[len(i)-1]) if i[len(i)-1] in string.punctuation else i for i in new_answer]

print ' '.join([answer[i] if i in answer.keys() else i for i in final_answer])

输出:

When you're going to drive it is important to always put on your seatbelts including your passengers If there are no lanes marked on the road you should drive in the 2 side of the road It's getting dark and the sun is fading you should turn on the light Before driving on a freeway you should make sure you have enough fuel oil water and the correct tyre pressure