【发布时间】:2014-04-09 08:47:08
【问题描述】:
我想创建一个函数,它遍历一个列表并创建一个字典,其中包含列表中每个事物的键以及列表中紧随键后的事物的值。
def function(s : str) -> {str:{str}}:
listt=list(s)
dictt= {}
for i in listt[:-1]:
if i not in dictt:
dictt[i] = set()
dictt[i].update(listt[listt.index(i)+1])
return dictt
print(function('bookeeper'))
应该返回:
{'b': {'o'}, 'k': {'e'}, 'p': {'e'}, 'o': {'o', 'k'}, 'e': {'e', 'p', 'r'}}
但实际上返回:
{'b': {'o'}, 'k': {'e'}, 'p': {'e'}, 'o': {'o'}, 'e': {'e'}}
【问题讨论】:
-
我记得几分钟前看到过这个问题:stackoverflow.com/questions/22956714/…
-
grate :) 你能帮帮我吗...
-
你用的是哪个python版本?
-
@user1153551:显然是 Python 3。函数注释给出了它。
-
@sshashank124:见Python 3 function definition reference,以及PEP 3107
标签: python dictionary