2

我正在寻找一个给定字符串列表的函数,我从单词所在的行中获取单词和索引:

例子

s = ['this is the first document',
'this is the second document',
'and this is a third document',
'perhaps there should be a fourth document',
'and now there is a fifth too']

当我应用我的功能时

def makeInverseIndex(s):

    dic={}
    index=0
    for line in s:
        set=line.split()
        for palabra in set:
            if palabra in dic:
                dic[palabra]=dic[palabra]+[index]
            else:
                dic[palabra]=[index]
        index+=1


    return dic

我正在获得

{'a': [2, 3, 4], 'first': [0], 'the': [0, 1], 'and': [2, 4], 'there': [3, 4], 'perhaps': [3], 'document': [0, 1, 2, 3], 'should': [3], 'is': [0, 1, 2, 4], 'be': [3], 'fourth': [3], 'third': [2], 'second': [1], 'too': [4], 'fifth': [4], 'now': [4], 'this': [0, 1, 2]}

但我想获得

{'a': {2, 3, 4}, 'first': {0}, 'the': {0, 1}, 'and': {2, 4}, 'there': {3, 4}, 'perhaps': {3}, 'document': {0, 1, 2, 3}, 'should': {3}, 'is': {0, 1, 2, 4}, 'be': {3}, 'fourth': {3}, 'third': {2}, 'second': {1}, 'too': {4}, 'fifth': {4}, 'now': {4}, 'this': {0, 1, 2}}

我必须在我的代码中更改什么?我已经阅读了 list 和 set 之间的区别,我使用 set 来尝试获取 {} 但它不起作用

谢谢你们

4

1 回答 1

0

使用dict.setdefault

def makeInverseIndex(s):
    dic={}
    for index, line in enumerate(s):  #use enumerate() for getting index as well as item
        words = line.split()
        for palabra in words:
            dic.setdefault(palabra,set()).add(index)

不要set用作变量名,因为它隐藏了内置函数set()

于 2013-07-07T20:45:57.737 回答