开发者

In list definition, can I use an else statement (for base case handling)?

开发者 https://www.devze.com 2023-02-18 09:35 出处:网络
what I\'d like to do is something like: sum([probabilities[ngram] for ngram in combination if probabilities.has_key(ngram)])

what I'd like to do is something like:

sum([probabilities[ngram] for ngram in combination if probabilities.has_key(ngram)]) 

this part is just fine. What I'd like to do is make something which handles a 'base case', that is to say assumes a certain probability, say .0001,

if not probabilities.has_key(ngram): 
    sum([probabilities[ngram] for ngram in combination if probabilities.has_key(ngram) else 开发者_C百科.0001])

Where probabilities is a dict with ntuples as keys and floats as values, combination is a list of ntuples.

edit fixed some brackets


No.

sum(probabilities.get(ngram, 0.0001) for ngram in combination)
0

精彩评论

暂无评论...
验证码 换一张
取 消