2
我被困在這個練習的另一部分。正在編碼的程序允許您鑽取短語(它給了你一段代碼,你寫出了英文翻譯),我對「轉換」功能的工作方式感到困惑。完整代碼:http://learnpythonthehardway.org/book/ex41.html在學習Python困難之路ex41中對函數感到困惑?
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
我很迷路。 w.capitalize()
是「w」還是文件本身,還是僅僅指的是列表中的對象?我也不確定.count()
函數爲什麼在.sample()
(或.sample()
確實是)的參數中。第一個for_loop的目的是什麼?
非常感謝您提供的任何幫助和幫助 - 我爲難以回答的問題感到抱歉。
如果您不確定標準庫中的某個函數的作用,請[查看](http://docs.python.org/2/library/random.html#random.sample)。 Python有很棒的文檔。 – voithos