python: add str and str.title() to list using list comprehension -
i reading in file words, this:
stop_words = [x x in open('stopwords.txt', 'r').read().split('\n')]
but need title() version of word in same list. can using 1 list comprehension?
in 1 (nested) list comprehension:
stop_words = [y x in open('stopwords.txt', 'r').read().split('\n') y in (x, x.title())]
edit: shouldn't this, because lose file object open file , can't close it. should use context manager:
with open('stopwords.txt', 'r') f: stop_words = [y x in f.read().split('\n') y in (x, x.title())]
Comments
Post a Comment