字典只返回部分答案(也许是关于缩进)

def main():

    file=[['mississippi', 'worth', 'reading','river'], ['commonplace', 'river', 'contrary', 'ways', 'remarkable']]

    print(set_and_count(file))


def set_and_count(common_deleted):

    sets=list(set(common_deleted[0]))

    for i in range(len(common_deleted)):

        ## make a non-repeated word list

        sets=list(set(sets+common_deleted[i]))

    ## initialize dict

    dict_wordloc={}

    for j in range(len(sets)):

        sublist=[]

        count_time=0

        for k in range(len(common_deleted)):

            if sets[j]  in common_deleted[k]:

                count_time+=1

                sublist.append(k)

                dict_wordloc[sets[k]]=count_time,sublist

    return(dict_wordloc)


main()

问题一:代码只返回部分答案


在示例输入文件中,字典中的键必须是 'mississippi'、'worth'、'reading'、'river'、'commonplace'、'contrary'、'ways'、'remarkable'


但是当我运行代码时,它返回:


{'remarkable': (2, [0, 1]), 'ways': (1, [0, 1])}

每次我运行它,返回的东西都不一样


例如,当我运行它两次时,结果是


{'contrary': (2, [0, 1]), 'ways': (1, [0, 1])}

问题 2:答案错误,在 Prob 1 的输出中,在 key 中remarkable,值必须是(1,[1])


元组中的第一项是有多少个句子有这个词(嵌套列表是一个句子)


元组中的第二项是包含该词的句子 NO


蝴蝶刀刀
浏览 118回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python