代码中的for语句和replace关键字
for statement and replace keyword in the code
我是 Python 的新手,实际上这是我的第 2 周,我正在努力按照这本书,学习 Python 艰难的道路。
对于ex41.py中的代码,我有两个问题。
不太明白为什么会有下面的说法:
for i in range(0, snippet.count("@@@")):
因为 snippet.count("@@@")
永远是 1,那么我只有 1 个值,也就是 0。为什么我们要遍历它呢?为什么不能直接设置变量param_count
和param_names
?
我也很困惑:
for word in param_names:
result = result.replace("@@@", word, 1)
我知道 param_names
是一个最多三个单词的单词列表,但是由于 @@@
在片段和短语中只出现一次,它如何被替换超过 1 次?
谢谢,
import random
from urllib import urlopen
import sys
WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []
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 '***'."
}
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
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[:]
for word in class_names:
result = result.replace("%%%", word, 1)
for word in other_names:
result = result.replace("***", word, 1)
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"
param_names
包含参数的后续名称,因此如果您有 ['param1', 'param2']
和一个字符串 '(@@@, @@@)'
您希望第一个替换为 result.replace("@@@", 'param1', 1)
所以它只会替换@@@
的第一次出现导致 '(param1, @@@)'
。然后,您想将第二个 @@@
替换为 '(param1, param2)'
。如果没有约束,您最终会得到 '(param1, param1)'
,即所有出现的 @@@
都将替换为第一个参数的名称。这同样适用于所有其他占位符(类 和 "others")。
对于某些特定的输入数据,有些东西看起来是多余的,但这并不意味着它通常是多余的。除了脚本中的示例,其他可能的输入都需要它,否则算法将不正确。
关于你的问题 2 (result = result.replace("@@@", word, 1)
):
一般不会更换超过1次。
对于可选参数“1”,只有第一个“@@@”会被替换。
例如:
str = "Hello @@@ hello @@@."
print str.replace("@@@", "world", 1)
print str.replace("@@@", "world", 2)
Hello world hello @@@.
Hello world hello world.
但在这种情况下它是一个循环,它在每个循环中替换一个项目。
str = "Hello @@@ hello @@@."
Loop1
str = "Hello world hello @@@."
Loop2
str = "Hello world hello world."
最后,如果您想知道为什么 @@@ 被一个以上的单词替换。
查看连接字符串方法:
param_names.append(', '.join(random.sample(WORDS, param_count)))
此方法的主要作用是将多个字符串转换为一个字符串。
print ', '.join(["Word1", "Word2"])
Word1, Word2
我是 Python 的新手,实际上这是我的第 2 周,我正在努力按照这本书,学习 Python 艰难的道路。
对于ex41.py中的代码,我有两个问题。
不太明白为什么会有下面的说法:
for i in range(0, snippet.count("@@@")):
因为
snippet.count("@@@")
永远是 1,那么我只有 1 个值,也就是 0。为什么我们要遍历它呢?为什么不能直接设置变量param_count
和param_names
?我也很困惑:
for word in param_names: result = result.replace("@@@", word, 1)
我知道
param_names
是一个最多三个单词的单词列表,但是由于@@@
在片段和短语中只出现一次,它如何被替换超过 1 次?
谢谢,
import random
from urllib import urlopen
import sys
WORD_URL = "http://learncodethehardway.org/words.txt"
WORDS = []
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 '***'."
}
PHRASE_FIRST = False
if len(sys.argv) == 2 and sys.argv[1] == "english":
PHRASE_FIRST = True
for word in urlopen(WORD_URL).readlines():
WORDS.append(word.strip())
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[:]
for word in class_names:
result = result.replace("%%%", word, 1)
for word in other_names:
result = result.replace("***", word, 1)
for word in param_names:
result = result.replace("@@@", word, 1)
results.append(result)
return results
try:
while True:
snippets = PHRASES.keys()
random.shuffle(snippets)
for snippet in snippets:
phrase = PHRASES[snippet]
question, answer = convert(snippet, phrase)
if PHRASE_FIRST:
question, answer = answer, question
print question
raw_input("> ")
print "ANSWER: %s\n\n" % answer
except EOFError:
print "\nBye"
param_names
包含参数的后续名称,因此如果您有 ['param1', 'param2']
和一个字符串 '(@@@, @@@)'
您希望第一个替换为 result.replace("@@@", 'param1', 1)
所以它只会替换@@@
的第一次出现导致 '(param1, @@@)'
。然后,您想将第二个 @@@
替换为 '(param1, param2)'
。如果没有约束,您最终会得到 '(param1, param1)'
,即所有出现的 @@@
都将替换为第一个参数的名称。这同样适用于所有其他占位符(类 和 "others")。
对于某些特定的输入数据,有些东西看起来是多余的,但这并不意味着它通常是多余的。除了脚本中的示例,其他可能的输入都需要它,否则算法将不正确。
关于你的问题 2 (result = result.replace("@@@", word, 1)
):
一般不会更换超过1次。
对于可选参数“1”,只有第一个“@@@”会被替换。
例如:
str = "Hello @@@ hello @@@."
print str.replace("@@@", "world", 1)
print str.replace("@@@", "world", 2)
Hello world hello @@@.
Hello world hello world.
但在这种情况下它是一个循环,它在每个循环中替换一个项目。
str = "Hello @@@ hello @@@."
Loop1
str = "Hello world hello @@@."
Loop2
str = "Hello world hello world."
最后,如果您想知道为什么 @@@ 被一个以上的单词替换。
查看连接字符串方法:
param_names.append(', '.join(random.sample(WORDS, param_count)))
此方法的主要作用是将多个字符串转换为一个字符串。
print ', '.join(["Word1", "Word2"])
Word1, Word2