0

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?

0

5 Answers 5

2

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
    • + : as many times possible
  • re.findall():

    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'] 
Sign up to request clarification or add additional context in comments.

Comments

1

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

2 Comments

This is the same as my answer? Just not a one-liner??
@0stone0 We answered at the same time.
0

The result you wants is a bit blurry, but here a few ways

  1. 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

  1. 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

Comments

0
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'] 

Comments

0

Here is a one-liner that will get the results you want.

s = 'john had a little blue car' print '\n'.join(' '.join([s.split()[i].strip(), s.split()[i+1] if i < len(s.split()) - 1 else '']) for i in range(0, len(s.split()), 2)) 

Output:

john had a little blue car 

Edit: Modified slightly to prevent errors when there are an odd number of words.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.