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