我有以下字符串:
>>>sentence='No, I shouldn't be glad, YOU should be glad.'
我想做一本字典,把句子中的一个词作为关键字,下一个词作为值。
>>>dict(sentence)
{('No,'): ['I'], ('I'): ['shouldn't'], ('shouldn't'): ['be'], ('be'): ['glad,', 'glad.'], ('glad,'): ['YOU'], ('YOU'): ['should'], ('should'): ['be']}
^ ^ ^
| | |
正如你所看到的,如果一个单词在一个句子中出现多次,它就会得到多个值。如果这是最后一个字,就不会加到字典里去了。glad'没有得到多个值,因为单词以','或''结尾,这使它成为一个不同的字符串。
最佳答案
import collections
sentence = "No, I shouldn't be glad, YOU should be glad."
d = collections.defaultdict(list)
words = sentence.split()
for k, v in zip(words[:-1], words[1:]):
d[k].append(v)
print(d)
这就产生了
defaultdict(<type 'list'>, {'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']})