python 3.3将句子转换为位置

时间:2016-01-10 20:19:48

标签: python

我需要这组代码才能将句子/句子2 /句子3转换成每个句子的单独位置,最好是以我所尝试的方法的simalar方法。 因此输出应该看起来像:

[1,2,3,4,5,6,7,8,9,1,3,8,9,5,6,7,4]

[1,2,3,4,5,6,7,3,4]

[1,2,3,4,5,6,7,8,5,1,11,12,13,14,8]

sentence = ("ask not what you can do for your country ask what your country can do for you")
sentence2 = ("some people enjoy computing others do not enjoy computing")
sentence3 = ("i will use this as my last sentence as i do not need another sentence")

d = {}
i = 0
values = []
for i, word in enumerate(sentence, sentence2, sentence3(" ")):
    if not word in d:
        d[word] = (i + 1)
    values += [d[word]]
print(values)

1 个答案:

答案 0 :(得分:1)

这解决了它

def conv2pos(*sentences):
    def conv_one(s):
        words = s.split()
        word_pos = {w:words.index(w) for w in set(words)}
        return [word_pos[w]+1 for w in words]

    return [conv_one(s) for s in sentences]

print(conv2pos(sentence, sentence2, sentence3))

至于它产生的给定句子

[
 [1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 3, 8, 9, 5, 6, 7, 4],
 [1, 2, 3, 4, 5, 6, 7, 3, 4],
 [1, 2, 3, 4, 5, 6, 7, 8, 5, 1, 11, 12, 13, 14, 8]
]

基本上,对于提供的每个句子,它将句子分成单词(在空格上)。

然后它会创建一个字典,用于将单词的第一个位置映射到每个唯一单词的句子中。

然后根据这样的字典创建句子中每个单词的位置列表。

注意:字典是可选的。它用于缓存每个单词的位置,并避免扫描列表中已经遇到过的单词。如果我们想删除它,我们可以做

def conv2pos(*sentences):
    def conv_one(s):
        words = s.split()
        return [words.index(w)+1 for w in words]

    return [conv_one(s) for s in sentences]