這是我想在 python 中使用 map 函式轉換它的代碼
for joint_word in joint_words:
if joint_word in wordset:
# word in joint_words found in sentence, just populate the index
wovec[i] = windex[joint_word]
else:
sim_word, max_sim = most_similar_word(joint_word, wordset)
if max_sim > ETA:
wovec[i] = windex[sim_word]
else:
wovec[i] = 0
i = i 1
uj5u.com熱心網友回復:
好吧,原始翻譯將是:
def lookup(word):
if word in wordset:
return windex[word]
sim_word, max_sim = most_similar_word(word, wordset)
return windex[sim_word] if max_sim > ETA else 0
wovec = list(map(lookup, joint_words))
如果wovec將用作可迭代物件,那么您不需要強制轉換為list.
uj5u.com熱心網友回復:
在 Python 3.8 中,您可以將Tim Roberts制作的簡單函式變成一行代碼:
wovec = list(map(lambda word: windex[word] if word in wordset else (windex[msw[0]] if ((msw := most_similar_word(word, wordset))[1]) > ETA else 0), joint_words))
基本上,不要解包 的結果most_similar_word(word, wordset),而是在運算式中分配它并按索引訪問。享受!
如果不清楚,我支持蒂姆的回答。請永遠不要使用像我寫的那樣的東西。更不用說理解通常比地圖略快。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/402080.html
上一篇:For回圈硒點擊
