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()
问题1: 代码只是返回答案的一部分
在示例输入文件中,字典中的键必须为 'mississippi','worth','reading','river','complaceplace,'contrary','ways','perfectable'
但是当我运行代码时,它返回:
{'remarkable': (2, [0, 1]), 'ways': (1, [0, 1])}
每次我运行它时,返回的东西都不一样
例如,当我运行两次时,结果是
{'contrary': (2, [0, 1]), 'ways': (1, [0, 1])}
问题2:
答案是错误的,在问题1的输出中,键remarkable
的值必须为(1,[1])
元组中的第一项是该单词有多少个句子(嵌套列表就是一个句子)
元组中的第二项是包含该单词的句子NO
答案 0 :(得分:1)
您可以改为使用嵌套的for
循环遍历单词,并使用单词作为键将enumerate
生成的索引附加到相应的dict条目:
d = {}
for i, l in enumerate(file):
for w in l:
d.setdefault(w, [0, []])[0] += 1
d[w][1].append(i)
d
将变为:
{'mississippi': [3, [0, 7, 8]], 'worth': [1, [0]], 'reading': [1, [0]], 'commonplace': [1, [1]], 'river': [4, [1, 2, 3, 6]], 'contrary': [1, [1]], 'ways': [1, [1]], 'remarkable': [1, [1]], 'considering': [1, [2]], 'missouri': [1, [2]], 'main': [1, [2]], 'branch': [1, [2]], 'longest': [1, [2]], 'seems': [1, [3]], 'safe': [1, [3]], 'crookedest': [1, [3]], 'part': [1, [3]], 'journey': [1, [3]], 'uses': [1, [3]], 'cover': [1, [3]], 'ground': [1, [3]], 'crow': [1, [3]], 'fly': [1, [3]], 'six': [1, [3]], 'seventy-five': [1, [3]], 'discharges': [1, [4]], 'water': [3, [4, 6, 7]], 'st': [1, [4]], 'lawrence': [1, [5]], 'twenty-five': [1, [5]], 'rhine': [1, [5]], 'thirty-eight': [1, [5]], 'thames': [1, [5]], 'vast': [1, [6]], 'drainage-basin': [2, [6, 8]], 'draws': [1, [6]], 'supply': [1, [6]], 'twenty-eight': [1, [6]], 'states': [1, [6]], 'territories': [1, [6]], 'delaware': [1, [6]], 'atlantic': [1, [6]], 'seaboard': [1, [6]], 'country': [1, [6]], 'idaho': [1, [6]], 'pacific': [1, [6]], 'slope': [1, [6]], 'spread': [1, [6]], 'forty-five': [1, [6]], 'degrees': [1, [6]], 'longitude': [1, [6]], 'receives': [1, [7]], 'carries': [1, [7]], 'gulf': [1, [7]], 'fifty-four': [1, [7]], 'subordinate': [1, [7]], 'rivers': [1, [7]], 'navigable': [2, [7, 7]], 'steamboats': [1, [7]], 'hundreds': [1, [7]], 'flats': [1, [7]], 'keels': [1, [7]], 'area': [1, [8]], 'combined': [1, [8]], 'areas': [1, [8]], 'england': [1, [8]], 'wales': [1, [8]], 'scotland': [1, [8]], 'ireland': [1, [8]], 'france': [1, [8]], 'spain': [1, [8]], 'portugal': [1, [8]], 'germany': [1, [8]], 'austria': [1, [8]], 'italy': [1, [8]], 'turkey': [1, [8]], 'almost': [1, [8]], 'wide': [1, [8]], 'region': [1, [8]], 'fertile': [1, [8]], 'valley': [1, [8]], 'proper': [1, [8]], 'exceptionally': [1, [8]]}
如果您希望将商品值设为元组,则可以使用以下方式转换d
:
d = {k: tuple(v) for k, v in d.items()}