Python -- about join and split, python -- joinsplit
. Join ()
Join splits the container object and connects the elements in the list with specified characters. Return a string (note: the elements in the container object must be of the character type)
>>> A = ['no', 'pain', 'No', 'gain']
>>> '_'. Join ()
'No _ pain_no_gain'
>>>
Note: The elements in the container object must be of the character type.
>>> B = ['I', 'am', 'No', 1]
>>> '_'. Join (B)
Traceback (most recent call last ):
File "<pyshell #32>", line 1, in <module>
'_'. Join (B)
TypeError: sequence item 3: expected string, int found
>>>
Dict is connected with Key values
>>> L = {'P': 'P', 'y': 'y', 't': 'T', 'H': 'h ', 'o': 'O', 'N': 'n '}
>>> '_'. Join (L)
'H _ o_n_p_t_y '# the disorder of dict, so that the elements are connected randomly. Same as set
>>>
. Split ()
In contrast to join, splitStringSplit into a single element (character type) and add it to the list. A List is returned.
>>> A = 'no _ pian_no_gain'
>>> A. split ('_')
['No', 'piany', 'No', 'gain']
>>>
Split can set the number of characters to be cut.
>>> A = 'no _ pian_no_gain'
>>> A. split ('_', 2)
['No', 'piany', 'no _ gain']
>>> A. split ('_', 1)
['No', 'pian _ no_gain']
>>> A. split ('_', 0)
['No _ pian_no_gain ']
>>> A. split ('_',-1)
['No', 'piany', 'No', 'gain']
>>>
It can be seen that the results returned by split ('_') and split ('_',-1) are consistent.