if/else in a list comprehension

后端 未结 11 898
一个人的身影
一个人的身影 2020-11-21 11:44

How can I do the following in Python?

row = [unicode(x.strip()) for x in row if x is not None else \'\']

Essentially:

  1. replace
11条回答
  •  耶瑟儿~
    2020-11-21 12:26

    You can combine conditional logic in a comprehension:

     ps = PorterStemmer()
     stop_words_english = stopwords.words('english')
     best = sorted(word_scores.items(), key=lambda x: x[1], reverse=True)[:10000]
     bestwords = set([w for w, s in best])
    
    
     def best_word_feats(words):
       return dict([(word, True) for word in words if word in bestwords])
    
     # with stemmer
     def best_word_feats_stem(words):
       return dict([(ps.stem(word), True) for word in words if word in bestwords])
    
     # with stemmer and not stopwords
     def best_word_feats_stem_stop(words):
       return dict([(ps.stem(word), True) for word in words if word in bestwords and word not in stop_words_english])
    

提交回复
热议问题