用另一个字符串替换python列表中的字符串

用另一个字符串替换python列表中的字符串,python,Python,目前,我的循环停止于用答案替换1。 我如何循环它,使它用4个答案替换所有4个空格 easyQuiz = "There are ___1___ countries in the world. The countries with the biggest landmass is ___2___. The coldest continent in the world is ___3___. ___4___ is the capital of China" blanks = ["___1___","_

目前,我的循环停止于用答案替换1。 我如何循环它,使它用4个答案替换所有4个空格

easyQuiz = "There are ___1___ countries in the world. The countries with the biggest landmass is ___2___. The coldest continent in the world is ___3___. ___4___ is the capital of China"

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

easyAnswer = ["195","Russia","Antartica","Beijing"]

#asks users to choose difficulty level
question = raw_input("Shall we start? Easy, Medium, Hard?")


if question == "Easy" or question == "easy":
    answerChoice = easyAnswer

answerChoice = answerList(question)
newprompt = []
newlist = []
index = 0
maxblanks = 3

for quizwords in difficulty(question).split():

    if quizwords == "1" :
        quizwords = quizwords.replace("1",answerChoice[index])


    elif quizwords == "2" :
        quizwords = quizwords.replace("2",answerChoice[index])

        index = index + 1
    newlist.append(quizwords)
    joinedlist = " ".join(newlist)

您可以
zip
使用相应的
easyAnswer
替换
循环中的
空格

for blank, answer in zip(blanks, easyAnswer):
    easyQuiz = easyQuiz.replace(blank, answer)
或者将
zip
转换为
dict
,并将
re.sub
与回调一起使用:

answer_dict = dict(zip(blanks, easyAnswer))
result = re.sub("___\d___", lambda m: answer_dict[m.group()], easyQuiz)

您可以
zip
使用相应的
easyAnswer
替换
循环中的
空格

for blank, answer in zip(blanks, easyAnswer):
    easyQuiz = easyQuiz.replace(blank, answer)
或者将
zip
转换为
dict
,并将
re.sub
与回调一起使用:

answer_dict = dict(zip(blanks, easyAnswer))
result = re.sub("___\d___", lambda m: answer_dict[m.group()], easyQuiz)

可以使用字符串格式

def easyQuiz(vals):
      return """
There are {} countries in the world.
The countries with the biggest landmass is {}.
The coldest continent in the world is {}.
{} is the capital of China""".format(*vals)

print(easyQuiz(("___1___","___2___","___3___","___4___")))
# There are ___1___ countries in the world.
# The countries with the biggest landmass is ___2___.
# The coldest continent in the world is ___3___.
# ___4___ is the capital of China

print(easyQuiz(("195","Russia","Antartica","Beijing")))
# There are 195 countries in the world.
# The countries with the biggest landmass is Russia.
# The coldest continent in the world is Antartica.
# Beijing is the capital of China

可以使用字符串格式

def easyQuiz(vals):
      return """
There are {} countries in the world.
The countries with the biggest landmass is {}.
The coldest continent in the world is {}.
{} is the capital of China""".format(*vals)

print(easyQuiz(("___1___","___2___","___3___","___4___")))
# There are ___1___ countries in the world.
# The countries with the biggest landmass is ___2___.
# The coldest continent in the world is ___3___.
# ___4___ is the capital of China

print(easyQuiz(("195","Russia","Antartica","Beijing")))
# There are 195 countries in the world.
# The countries with the biggest landmass is Russia.
# The coldest continent in the world is Antartica.
# Beijing is the capital of China

您可以将字符串格式与
regex
一起使用:

import re
easyQuiz = "There are ___1___ countries in the world. The countries with the biggest landmass is ___2___. The coldest continent in the world is ___3___. ___4___ is the capital of China"
easyAnswer = ["195","Russia","Antartica","Beijing"]
answers = re.sub('___\d+___', '{}', easyQuiz).format(*easyAnswer)
输出:

'There are 195 countries in the world. The countries with the biggest landmass is Russia. The coldest continent in the world is Antartica. Beijing is the capital of China'

您可以将字符串格式与
regex
一起使用:

import re
easyQuiz = "There are ___1___ countries in the world. The countries with the biggest landmass is ___2___. The coldest continent in the world is ___3___. ___4___ is the capital of China"
easyAnswer = ["195","Russia","Antartica","Beijing"]
answers = re.sub('___\d+___', '{}', easyQuiz).format(*easyAnswer)
输出:

'There are 195 countries in the world. The countries with the biggest landmass is Russia. The coldest continent in the world is Antartica. Beijing is the capital of China'


这个问题有点让人困惑,尤其是“难度”和“答案列表”。假设“answerList”返回一个包含四个答案的列表。您的循环将取决于难度(问题).split()。我的预感是,您的循环只迭代了一次,之后索引不会更新,因此循环停止在用答案替换1

这个问题有点让人困惑,尤其是“难度”和“答案列表”。假设“answerList”返回一个包含四个答案的列表。您的循环将取决于难度(问题).split()。我的预感是,您的循环只迭代了一次,之后索引不会更新,因此循环停止在用答案替换1

什么是
answerList
难度
?你可能想在这里使用字符串格式:另外,如果
quizwords==“1”
,你可以只做
quizwords=answerChoice[index]
而不是
replace
,但这真的是你想要的吗?如果你需要任何帮助,发布一个包含预期结果的MCVE()什么是
answerList
难度
?你可能想在这里使用字符串格式:另外,如果
quizwords==“1”
,你可以只做
quizwords=answerChoice[index]
而不是
replace
,但这真的是你想要的吗?如果你需要任何帮助,发布一个包含预期结果的MCVE()非常感谢。.format(*vals)中的*是什么意思?@nicholasgwee不客气。是的。我可以问一下,虽然问题中有四个{},但我怎样才能只替换第二个呢values@nicholasgwee作为一种简单的方法,您仍然可以替换所有4个,但对于1、2、4,只需插入占位符即可。例如,
print(EasyQuike(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu!非常感谢!非常感谢。.format(*vals)中的*是什么意思?@nicholasgwee不客气。是的。我可以问一下,虽然问题中有四个{},但我怎样才能只替换第二个呢values@nicholasgwee作为一种简单的方法,您仍然可以替换所有4个,但对于1、2、4,只需插入占位符即可。例如,
print(EasyQuike(“\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu!非常感谢!