I have a sentence
s = 'john had a little blue car'
which I want to split into three different strings (of word length 2) to obtain
'john had'
'a little'
'blue car'
How can I do this using split? First I tried splitting via
s.split(" ")
to give
['john', 'had', 'a', 'little', 'blue', 'car']
how can I now take that list of words and join them to obtain the result I want?
CodePudding user response:
Regex
Using a regex:
import re
r = r"(\w \s \w )"
s = "john had a little blue car"
m = re.findall(r, s)
(\w \s \w )
Regex101 Demo\w
: matches any word character\s
: matches any whitespace
-
Return all non-overlapping matches of pattern in string, as a list of strings or tuples.
Try it online!
Python
A native Python way is to use the s.split()
as you described, and then 'splice' the array and join()
them back together in groups of 2 using a for
loop with range(0, len(s), 2)
to take steps of 2:
s = 'john had a little blue car'
s = s.split(" ")
s = [' '.join(s[i : i 2]) for i in range(0, len(s), 2)]
Try it online!
Both result in:
['john had', 'a little', 'blue car']
CodePudding user response:
Not the fastest way to be honest:
s = 'john had a little blue car'.split()
for i in range(0, len(s), 2):
print(f"{s[i]} {s[i 1]}")
Where: s[i]
- first elem of pair, s[i 1]
- second elem of a pair
CodePudding user response:
The result you wants is a bit blurry, but here a few ways
Using for loop to iterate over the string and combining pairs of words together:
s = 'john had a little blue car' words = s.split() for i in range(0, len(words), 2): w1 = words[i] w2 = words[i 1] print(w1, w2)
if you want to save the result than :
s = 'john had a little blue car'
split_list = []
words = s.split()
for i in range(0, len(words), 2):
w1 = words[i]
w2 = words[i 1]
split_list.append([w1, w2])
after the code you will have in split_list in each cell the 2 of the words
Using the zip function to iterate over the string and combining pairs of words together (Might not familiar with Zip but its a very helpful tool):
s = 'john had a little blue car' words = s.split() for w1, w2 in zip(words[::2], words[1::2]): print(w1, w2)
if you want to save the result than :
s = 'john had a little blue car'
split_list = []
words = s.split()
for w1, w2 in zip(words[::2], words[1::2]):
split_list.append([w1, w2])
after the code you will have in split_list in each cell the 2 of the words
CodePudding user response:
s = 'john had a little blue car'.split(' ')
#['john', 'had', 'a', 'little', 'blue', 'car']
s[0::2]
#['john', 'a', 'blue']
s[1::2]
#['had', 'little', 'car']
list(zip(s[0::2],s[1::2]))
#[('john', 'had'), ('a', 'little'), ('blue', 'car')]
Or for desired output:
[' '.join(x) for x in zip(s[0::2],s[1::2])]
#['john had', 'a little', 'blue car']