开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜