Python for-in-loop如何处理多个序列
我遇到了以下for循环代码,但不太清楚它是如何运行的:Python for-in-loop如何处理多个序列,python,for-loop,Python,For Loop,我遇到了以下for循环代码,但不太清楚它是如何运行的: for sentence in snippet, phrase: result = sentence[:] 它是通过“片段”然后是“短语”进行迭代的吗 编辑: PHRASES = { "class %%%(%%%):": "Make a class named %%% that is-a %%%.", "class %%%(object):\n\tdef __init__(self, ***)" : "class %%%
for sentence in snippet, phrase:
result = sentence[:]
它是通过“片段”然后是“短语”进行迭代的吗
编辑:
PHRASES = {
"class %%%(%%%):":
"Make a class named %%% that is-a %%%.",
"class %%%(object):\n\tdef __init__(self, ***)" :
"class %%% has-a __init__ that takes self and *** parameters.",
"class %%%(object):\n\tdef ***(self, @@@)":
"class %%% has-a function named *** that takes self and @@@ parameters.",
"*** = %%%()":
"Set *** to an instance of class %%%.",
"***.***(@@@)":
"From *** get the *** function, and call it with parameters self, @@@.",
"***.*** = '***'":
"From *** get the *** attribute and set it to '***'."
}
##############
#'snippet' is a key in the dict shown above, and 'phrase' is its corresponding value
def convert(snippet, phrase):
class_names = [w.capitalize() for w in
random.sample(WORDS, snippet.count("%%%"))]
other_names = random.sample(WORDS, snippet.count("***"))
results = []
param_names = []
for i in range(0, snippet.count("@@@")):
param_count = random.randint(1,3)
param_names.append(', '.join(random.sample(WORDS, param_count)))
########
#Here is the code in question
########
for sentence in snippet, phrase:
result = sentence[:]
# fake class names
for word in class_names:
result = result.replace("%%%", word, 1)
# fake other names
for word in other_names:
result = result.replace("***", word, 1)
# fake parameter lists
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
否。
snippet,phrase
仅定义这两个元素的元组。迭代在该元组之上;ie语句首先是片段的值,然后是短语的值。它不会遍历这些值的内容。请包含一个更完整的示例。对于(代码段、短语)中的句子,它与相同。
<代码>句子
将在第一次迭代中设置为片段
,在第二次迭代中设置为短语
。你可以在大约5秒钟内测试出来。你说“句子是…的第一个值”,然后你说“它不会遍历…的内容”。内容和价值有什么区别?