【发布时间】:2018-04-10 02:02:59
【问题描述】:
所以我这里有这段代码,但是当我到达最后一个 for 循环时,它只返回一个单词,而不是字典其余部分的 x 和 * 数量。任何帮助表示赞赏
def main():
print(parse_string("I had a good dog not a cat. A dog eats pizzas. A dog is happy. There is a happy dog there in the dog park."))
def parse_string(string):
dicto = {}
ast = ['*']
x = ['X']
boring = ['to', 'the', 'and', 'i', 'of', 'he', 'she',
'a', "ill", "ive", 'but', 'by', 'we', 'whose'
, 'how', 'go', 'such', 'this', 'me', 'can', "shes", "hes"
, 'have', 'has', 'had', 'an', 'did', 'so', 'to', "well", 'on'
, 'him', 'well', 'or', 'be', 'as', 'those', 'there', 'are', 'do'
, 'too', 'if', 'it', 'at', 'what', 'you', 'will', 'in', 'with'
, 'not', 'for', 'is', 'my', 'o', 'her', 'his', 'am']
newstring = string.lower()
newstring = newstring.replace('.', '')
newstring = newstring.replace("'", '')
finalstring = newstring.split()
for word in finalstring:
if word not in boring:
if word not in dicto:
dicto[word] = 1
else:
dicto[word] += 1
for wrd in dicto:
xmult = dicto[wrd] // 5
astmult = dicto[wrd] % 5
if xmult >= 1:
return wrd + " " + xmult*x[0] + " " + astmult*ast[0]
else:
return wrd + " " + astmult*ast[0]
if __name__ == '__main__':
main()
【问题讨论】:
-
您能否详细说明这段代码正在做什么以及它应该做什么?如果人们不必为了理解你写的东西而研究它,你就更有可能得到一个合理的答案。
标签: python loops dictionary for-loop