python beginner note of the list comprehensions


List comprehensions (also known analytical formula list) provides a method to create a concise list.
Note: in short, the conventional multi-line for circulating compressed into a line of code,
which applies to the compression syntax list, dictionaries, collection and other data structures may iterate (iterables) .

Create an empty list, remove an even number between 1-10.
When not in use list comprehensions of:
one_list = []
for i in range(1, 11):
    if i%2 ==0:
        one_list.append(i)
print(one_list)  # 打印结果:[2, 4, 6, 8, 10]

 

If you are using a list comprehension:
two_list = [i for i in range(1, 11) if i%2 ==0 ]
print(two_list)  # 打印结果:[2, 4, 6, 8, 10]

 

 

 

Guess you like

Origin www.cnblogs.com/jszfy/p/11117374.html