gpt4 book ai didi

python - 在 "in"之后有 2 个变量的循环

转载 作者:行者123 更新时间:2023-11-28 20:41:05 24 4
gpt4 key购买 nike

我对下面的说法感到困惑

for sentence in snippet, phrase. 

为什么“in”后面有两个项目

完整代码

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)))

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

最佳答案

这只是一个两次循环。在第一次迭代中,sentence 设置为 snippet。在第二次迭代中,sentence 设置为 phrase

这是一个简单的例子:

>>> for x in "a", "b":
... print(x)
...
a
b
>>>

关于python - 在 "in"之后有 2 个变量的循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34327905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com