4

我有以下字符串:

>>>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' 不会获得多个值,因为单词以 ',' 或 '.' 结尾 这使它成为一个不同的字符串。

4

5 回答 5

4
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']})
于 2012-12-10T18:11:58.293 回答
3

使用dict.setdefault()

In [9]: strs = "No, I shouldn't be glad, YOU should be glad."


In [19]: dic = {}

In [20]: for x, y in zip(words, words[1:]):
      dic.setdefault(x, []).append(y)     
   ....:     

In [21]: dic
Out[21]: 
{'I': ["shouldn't"],
 'No,': ['I'],
 'YOU': ['should'],
 'be': ['glad,', 'glad.'],
 'glad,': ['YOU'],
 'should': ['be'],
 "shouldn't": ['be']}
于 2012-12-10T18:15:58.473 回答
0

这未经测试,但应该接近。

words = sentence.split()
sentenceDict = {}
for index in xrange(len(words)-1):
    if words[index] in sentenceDict:
        sentenceDict[words[index].append(words[index+1])
    else
        sentenceDict[words[index]] = [words[index+1]]
于 2012-12-10T18:14:13.213 回答
0

如果顺序不重要,只是另一种方法

sentence="No, I shouldn't be glad, YOU should be glad."
#Split the string into words
sentence = sentence.split()
#Create pairs of consecutive words
sentence = zip(sentence,sentence[1:])
from itertools import groupby
from operator import itemgetter
#group the sorted pairs based on the key
sentence = groupby(sorted(sentence, key = itemgetter(0)), key = itemgetter(0))
#finally create a dictionary of the groups
{k:[v for _,v in  g] for k, g in sentence}
{'No,': ['I'], 'be': ['glad,', 'glad.'], 'glad,': ['YOU'], 'I': ["shouldn't"], 'should': ['be'], "shouldn't": ['be'], 'YOU': ['should']}
于 2012-12-10T18:31:33.007 回答
0
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']})

@NLS:我只是想在此添加一些内容。“d = collections.defaultdict(list)”,就像 dict 对象不保留单词的顺序,所以如果我们必须保留句子的顺序,我们可能必须使用元组。

于 2012-12-10T18:37:23.613 回答