簡體   English   中英

Python:在列表中分離術語並分配值

[英]Python : Separating the terms in a list and assigning values

我使用了關鍵字提取器,並獲得了如下列表:

[('solutions design team', 0.5027793039863974), 
('communication skills', 0.039048703166463736), 
('internal stakeholders', 0.03230578820017667),
('potential customers', 0.020380881551651655), ('utilize', 0.002776174060064261)]

我正在嘗試分離每個單詞,並分別為每個單詞分配相應的值(在右側給出)。

例如-將“解決方案設計團隊” = 0.5027793039863974放入

'solutions' = 0.5027793039863974, 
'design' = 0.5027793039863974 ,   
'team' = 0.5027793039863974.

使用double flat comprehension重新創建帶有分隔詞的元組列表的方法:

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

詞典理解,詞典更新可以通過以下方式幫助您

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'])

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM