Python: List Comprehensions

If I'd like to create a list based on the existing objects (maybe a list or a sequence of numbers), it's a good way to achieve the goal. I often use this to build a list of files. For example,

output = [open('output_%s' %(i), 'w+') for i in range(6, 10, 1)]

The program will generate four files named output_6, output_7, output_8, and output_9.


Reference:
http://www.swaroopch.com/notes/python/#list_comprehension

留言

這個網誌中的熱門文章

Python: Using pydot and Graphviz to generate PDF file for Decision Tree