循环如何使用多个序列python

时间:2016-08-22 14:54:38

标签: python for-loop

我遇到了以下for循环代码,并不太确定它是怎么回事:

for sentence in snippet, phrase:
    result = sentence[:]

是通过'snippet'然后'短语'来迭代?

编辑:

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

1 个答案:

答案 0 :(得分:1)

没有。 snippet, phrase仅定义这两个元素的元组。迭代结束了那个元组;即句子首先是snippet的值,然后是phrase的值。它不会遍历这些值的内容。