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

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -