In many sequential containers , The most basic container is the list (list), Not only can it be modified, but it can incorporate different data types . I believe that everyone has learned the basics of it , But what's a little difficult is list parsing (list comprehension) 了 . A powerful way to build lists , But because of the unfamiliar grammar , Or don't understand , Its effect has been underestimated . To master list parsing is to open the door to generating expressions , Generating expressions can be used for other purposes , It can generate elements to fill any type of sequence . So understand their role by comparing list parsing expressions with generating expressions .
List of analytical (List Comprehensions) And generating expressions (Generator Expressions)
stay Python in , For lists, the fastest way to construct a list is list parsing , Other containers use generated expressions . If you don't use these grammatical functions in your daily life , You've missed a great opportunity , Because you can write the best code and the most understandable format . If you still suspect this , Please keep reading , I'll explain it to you . For the sake of simplicity , People often write list parsing as :listcomps, Write the generated expression as :genexps.
List parsing and readability
Compare the following two examples , Which way do you think is more