简体   繁体   中英

Creating a Python Dictionary: Syntax Problems

Hopefully this will be a quick solution. I'm afraid that I don't quite know enough about Python to figure out why I'm having a compiler syntax error. The code is as follows, and the compiler has an issue with the "for" statement in the line of code:

vocab_counter = {vocabLine.strip().lower(): 0 for vocabLine in vocabOutFile}

vocabLine is a line of text from a .txt document, and vocabOutFile is the file that it's reading from. I am trying to create a list of the words in the .txt document and increment a counter on each word as they're found in a separate document.

So why is there a syntax error?

Thank you all for your help, I really appreciate it!

Sorry for the bother. I found out that what I was trying to do is only available in python 2.7 or higher. For python 2.6 I had to change the code to:

vocab_counter = dict((vocabLine.strip().lower(), 0)
                     for vocabLine in vocabOutFile)

Thanks for your time!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM