0
我无法看到打印结果的模式。我已经尝试过这种其他的解决方案,但无法得到它的权利=(我怎样才能像原始字符串这样的字典排序?
s = "hi hi hi, how are you? you you"
print(s)
s = s.replace('?','')
s = s.replace('!','')
s = s.replace(',','')
s = s.replace('.','')
l = s.split()
d = {}
for i, termo in enumerate(l):
if not d.get(termo):
d[termo] = []
d[termo].append(i+1)
print ('d:', d)
输出示例:
d: {'you': [6, 7, 8], 'how': [4], 'are': [5], 'hi': [1, 2, 3]}
d: {'are': [5], 'hi': [1, 2, 3], 'how': [4], 'you': [6, 7, 8]}
词典本质上是无序的。使用'collections.OrderedDict'。 –
你的目标是什么?该输出是预期的输出还是期望的输出? – James
请阅读http://stackoverflow.com/questions/15479928/why-is-the-order-in-python-dictionaries-and-sets-arbitrary – cdarke