declaration - run time variable access in python -
i new python. here testing on example varialbe wordlist
has been used in loop.
do need declare manually before using it? or declared runtime?
i tried manual declaration remains empty.
i following example: http://www.sjwhitworth.com/sentiment-analysis-in-python-using-nltk/
if directly use this:
wordlist = [i in wordlist if not in stopwords.words('english')] wordlist = [i in wordlist if not in customstopwords]
it gives error:
wordlist = [i in wordlist if not in stopwords.words('english')] nameerror: name 'wordlist' not defined
i manually declared wordlist
this
wordlist = []
but remain empty in case:
wordlist = [] wordlist = [i in wordlist if not in stopwords.words('english')] wordlist = [i in wordlist if not in customstopwords] print wordlist
what wrong doing here?
here's how list comprehensions work in python. have list x
like
x=[1,2,3,4]
and increment values using list comprehension:
y=[element+1 element in x] # ^ ^ ^ #element element list on #to added operations #in list of y performed
this outputs:
y=[2,3,4,5]
in case x
(i.e., wordlist
) empty so, for
loop not iterate. according mentioned link's description wordlist
should array of artist names.
wordlist = ["justin timberlake", "tay zonday", "rebecca black"]
Comments
Post a Comment