[Python] Summary of how to use split and join functions
Divide the character string by the specified characters to make a list.
Object .split ("split character ")
└ Characters in the argument are deleted
└ Object is a character string * Numerical value is an error
└ Not destructive * The original object remains as it is
└ If the argument is empty, split it with a space
Split with whitespace
s="1 2 3 4 5 6"
l = s.split()
print(l)
#output
['1', '2', '3', '4', '5', '6']
Specify a space as an argument
s="1 2 3 4 5 6"
l = s.split(" ")
print(l)
#output
['1', '2', '3', '4', '5', '6']
error
i=123456
l = i.split()
AttributeError: 'int' object has no attribute 'split'
python
s="1,2,3,4,5,6"
l = s.split(",")
print(l)
#output
['1', '2', '3', '4', '5', '6']
text
s="I have a pen !"
l = s.split()
print(l)
#output
['I', 'have', 'a', 'pen', '!']
Delimiter removed
s="I have a pen !"
l = s.split("a")
print(l)
#output
['I h', 've ', ' pen !']
split ('separator', integer)
└ Divide into integers
└ Maximum index number becomes an integer
python
s="1 2 3 4 5 6"
l = s.split(" ",2)
print(l)
#output
['1', '2', '3 4 5 6']
Element specification
s="1 2 3 4 5 6"
l = s.split(" ",2)
print(l[0])
print(l[2])
#output
1
3 4 5 6
Output the 0th element and the 2nd element. l [3] is out of range (IdexError: list index out of range)
rsplit ('separator', integer)
└ Separate from the back by an integer
└ Maximum index number becomes an integer
python
s="1 2 3 4 5 6"
ls = s.split(" ", 2)
lr = s.rsplit(" ", 2)
print(ls)
print(lr)
#output
['1', '2', '3 4 5 6']
['1 2 3 4', '5', '6'] #rsplit separates from behind
Extracting elements
s="1 2 3 4 5 6"
ls = s.split(" ", 2)
lr = s.rsplit(" ", 2)
#Output the 0th element
print(ls[0])
print(lr[0])
#output
1
1 2 3 4
No number of delimiters specified
s="1 2 3 4 5 6"
ls = s.split()
lr = s.rsplit()
print(ls)
print(lr)
#output
['1', '2', '3', '4', '5', '6']
['1', '2', '3', '4', '5', '6']
'Join character'.join (list)
└ The element of list is a character string
└ An error will occur if the element is not a character string
└ Not destructive (object remains intact)
python
l = ['I', 'have', 'a', 'pen', '!']
#「-Combine with
s1 = "-".join(l)
print(s1)
#Combine with whitespace
s2 = " ".join(l)
print(s2)
#output
I-have-a-pen-!
I have a pen !
** ▼ An error will occur if the element is not a character string **
python
l=[1,2,3,4]
s = "-".join(l)
#output
TypeError: sequence item 0: expected str instance, int found
(1) Convert to a character string with the map function. (2) Convert to a character string with a for statement.
Convert elements from numbers to strings with the map function and join them with join.
python
l=[1,2,3,4]
ls = map(str, l)
s = "-".join(ls)
print(s)
#output
1-2-3-4
map (processing, iterable)
-Perform processing one for each element of iterable.
-If you specify a type for processing, you can convert to that type.
-Output is map type (example: <map at 0x2d00fce8b80>)
・ Not destructive.
** ▼ Convert to a character string with map **
map (str, iterable)
Convert the numbers in list to strings
l=[1,2,3,4]
s = map(str, l)
print(list(s))
#output
['1', '2', '3', '4']
python
l=[1,2,3,4]
s = "-".join([str(i) for i in l])
print(s)
#output
1-2-3-4
・ Inclusive notation
[Processing for Variables in Iterable]
└ The processing result is returned as a list.
Recommended Posts