2 回答
TA贡献1883条经验 获得超3个赞
如何使用双重平面理解重新创建一个带有分隔词的元组列表:
inlist = [('solutions design team', 0.5027793039863974),
('communication skills', 0.039048703166463736),
('internal stakeholders', 0.03230578820017667),
('potential customers', 0.020380881551651655), ('utilize', 0.002776174060064261)]
outlist = [(word,value) for words,value in inlist for word in words.split()]
结果:
>>> outlist
[('solutions', 0.5027793039863974),
('design', 0.5027793039863974),
('team', 0.5027793039863974),
('communication', 0.039048703166463736),
('skills', 0.039048703166463736),
('internal', 0.03230578820017667),
('stakeholders', 0.03230578820017667),
('potential', 0.020380881551651655),
('customers', 0.020380881551651655),
('utilize', 0.002776174060064261)]
请注意,如果关键字出现多次,则元组列表中将出现重复项。如果你想累积它们,一个collections.defaultdict(float)对象可以很方便地创建一个带有关键字=>累积值的字典。
accumulated = collections.defaultdict(float)
for word,value in outlist:
accumulated[word] += value
TA贡献1878条经验 获得超4个赞
字典理解,字典更新可以通过以下方式帮助您
corpus = [('solutions design team', 0.5027793039863974), ('communication skills', 0.039048703166463736), ('internal stakeholders', 0.03230578820017667), ('potential customers', 0.020380881551651655), ('utilize', 0.002776174060064261)]
final_dict = {}
for phase, prob in corpus:
final_dict.update({word:prob for word in phase.split()}
print(final_dict['solutions'])
print(final_dict['design'])
添加回答
举报