【发布时间】:2022-01-01 13:28:10
【问题描述】:
我有一个这样的单词列表:
word_list=[{"word": "python",
"repeted": 4},
{"word": "awsome",
"repeted": 3},
{"word": "frameworks",
"repeted": 2},
{"word": "programing",
"repeted": 2},
{"word": "stackoverflow",
"repeted": 2},
{"word": "work",
"repeted": 1},
{"word": "error",
"repeted": 1},
{"word": "teach",
"repeted": 1}
]
,来自另一个笔记列表:
note_list = [{"note_id":1,
"note_txt":"A curated list of awesome Python frameworks"},
{"note_id":2,
"note_txt":"what is awesome Python frameworks"},
{"note_id":3,
"note_txt":"awesome Python is good to wok with it"},
{"note_id":4,
"note_txt":"use stackoverflow to lern programing with python is awsome"},
{"note_id":5,
"note_txt":"error in programing is good to learn"},
{"note_id":6,
"note_txt":"stackoverflow is very useful to share our knoloedge"},
{"note_id":7,
"note_txt":"teach, work"},
]
我想知道如何将每个单词映射到它自己的注释:
maped_list=[{"word": "python",
"notes_ids": [1,2,3,4]},
{"word": "awsome",
"notes_ids": [1,2,3]},
{"word": "frameworks",
"notes_ids": [1,2]},
{"word": "programing",
"notes_ids": [4,5]},
{"word": "stackoverflow",
"notes_ids": [4,6]},
{"word": "work",
"notes_ids": [7]},
{"word": "error",
"notes_ids": [5]},
{"word": "teach",
"notes_ids": [7]}
]
我的工作:
# i started by appending all the notes text into one list
notes_test = []
for note in note_list:
notes_test.append(note['note_txt'])
# calculate the reptition of each word
dict = {}
for sentence in notes_test:
for word in re.split('\s', sentence): # split with whitespace
try:
dict[word] += 1
except KeyError:
dict[word] = 1
word_list= []
for key in dict.keys():
word = {}
word['word'] = key
word['repeted'] = dict[key]
word_list.append(word)
我的问题:
- 如何映射单词列表和笔记列表来获取映射列表
- 你如何发现我的代码质量,任何备注
【问题讨论】:
-
你问这个问题的方式很混乱。我想你想问的是:“我有一个笔记列表,我需要计算每个单词的频率,以及它所在的笔记列表”。对吗?
-
是的,类似的,我已经设法计算出频率,但我对笔记列表感到困惑
标签: python dictionary mapping