What does Python mean? The syntax is a bit strange?
怪我咯
怪我咯 2017-06-22 11:52:31
0
4
714
filelist = [x for x in os.listdir() if os.path.isfile(x)]
怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(4)
女神的闺蜜爱上我

Equivalent to:

filelist = []
for x in os.listdir():
    if os.path.isfile(x):
        filelist.append(x)
    

This is the content of list parsing.

扔个三星炸死你

List generation
is equivalent to. os.listdir() requires a parameter, which path you want to get the list.


In [54]: filelist = []

In [55]: for x in os.listdir("."):
    ...:     if os.path.isfile(x):
    ...:         filelist.append(x)
    ...:

In [56]:

Using list generation is a little faster than [].append(). You can test it using large batches. Why fast. The mechanism of the python listobject model is related. If you have time, you can read the python source code, which is written in C language

过去多啦不再A梦

Definition:
List comprehensions (also known as list comprehensions) provide a concise and concise way to create lists.

Specification:
variable = [out_exp for out_exp in input_list if out_exp == 2]

Example:
multiples = [i for i in range(30) if i % 3 is 0]
print(multiples)

Output: [0, 3, 6, 9, 12, 15, 18, 21, 24, 27]

Scenario:
When the logic in the loop is relatively simple, it can be replaced by derivation to increase code readability and cleanliness

为情所困

List comprehensions
List generation

The order is as follows:

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template