This article is to introduce the data in Python in a centralized way, including list parsing, dictionary parsing, set parsing, and attached to the relevant examples, the need for small partners can refer to.
Import OS; --Python self-belt
Print (OS.GETCWD ())--Get current working directory
Os.chdir ('/users/longlong/documents ')--Convert to/users/longlong/documents directory
Os.path.join (Parm1, Parm2,...)--Constructs a path name from one or more path fragments.
Os.path.expanduser ()--to extend the path containing the ~ symbol to the full path
Copy code code as follows:
>>> pathname = '/users/pilgrim/diveintopython3/examples/humansize.py '
>>> os.path.split (Pathname)--('/users/pilgrim/diveintopython3/examples ', ' humansize.py ') List contents construct absolute path
Copy code code as follows:
>>> Import OS
>>> print (OS.GETCWD ())
/users/longlong/documents
>>> Os.chdir ("./python/")
>>> OS.GETCWD ()
'/users/longlong/documents/python '
>>> Print (Os.path.realpath (' whileloop.py ')
)
/users/longlong/documents/python/whileloop.py
>>> Print (Os.path.realpath ("whileloop.py"))
/users/longlong/documents/python/whileloop.py
>>>
List resolution
Copy code code as follows:
>>> [Os.path.realpath (f) for F in Glob.glob ("*.py")]
['/users/longlong/documents/python/indices_over_two_objects.py ', '/users/longlong/documents/python/loops_over_ indices.py ', '/users/longlong/documents/python/while_loops.py ']
>>> [F for F in Glob.glob ("*.py") if Os.stat (f). st_size > 700]
[' indices_over_two_objects.py ', ' while_loops.py ']
1
Dictionary parsing
?
1 2 3 4 5 6 7 8 9 10 |
>>> metadat_dict = {F:os.stat (f) for F in Glob.glob (' *.py ')} >>> type (metadat_dict) <class ' Dict ' & Gt >>> list (Metadat_dict.keys ()) [' indices_over_two_objects.py ', ' while_loops.py ', ' loops_over_indices.py '] >>> metadat_dict[' indices_over_two_objects.py '].st_size 871 |
?
1 2 3 4 5 6 |
>>> list (Metadat_dict.keys ()) [' indices_over_two_objects.py ', ' while_loops.py ', ' loops_over_indices.py '] >>> a_dict = {' A ': 1, ' B ': 2, ' C ': 3} >>> {Value:key for key,value in A_dict.items ()} {1: ' A ', 2: ' B ', 3: ' C '} |
Set parsing
?
1 2 3 4 5 6 7 8 9 10 11-12 |
>>> A_set = set (range) >>> a_set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} >>> {x**2 for x in A_set} {0, 1, 9, 4, >>> {x for x in A_set if x%2 = = 0} {0, 8, 2, 4, 6} >>> {2**x For x in range (10)} {32, 1, 2, 64, 4, 128, 256, 512, 8, 16} |
The above mentioned is the entire content of this article, I hope you can enjoy.