【发布时间】:2018-09-28 19:45:09
【问题描述】:
给出的指示要求返回字符串中每个单词的长度的代码。所以就像它会计算每个单词中的字母数量并将其打印在单词旁边 我有这个代码:
def word_lengths(a):
a = a.lower()
c = list(a)
a = ""
for x in c:
if x == "," or x == "." or x == "'" or x == "!" or x == "?":
c[c.index(x)] = ""
for x in c:
a += x
y = a.split()
z = {}
for x in y:
z[x] = len(x)
return z
print(word_lengths("I ate a bowl of cereal out of a dog bowl today."))
返回:
{'dog': 3, 'bowl': 4, 'a': 1, 'out': 3, 'of': 2, 'ate': 3, 'cereal': 6, 'i': 1, 'today': 5}
【问题讨论】:
-
你已经建立了一个反转你的结果的字典。
标签: python string python-3.x list dictionary