-2

Basically I have a string like this:

names = "John Doe Jane Doe John Smith Jane Smith". 

I want to convert it to look like:

outs =  ["John Doe", "Jane Doe", "John Smith", "Jane Smith"]. 

Thanks.

Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
bigdee
  • 1
  • `s.split()` can separate the string by space. But it seems that your rule is not simply by space. – Haoliang Jun 30 '22 at 18:04
  • 1
    Welcome to stack overflow, What have you tried so far, and what went wrong with your attempts? – G. Anderson Jun 30 '22 at 18:06
  • is string format is same, like `first_name last_name first_name last_name ....` ? – sahasrara62 Jun 30 '22 at 18:06
  • 2
    Does this answer your question? [Is there a way to split a string by every nth separator in Python?](https://stackoverflow.com/questions/1621906/is-there-a-way-to-split-a-string-by-every-nth-separator-in-python) – G. Anderson Jun 30 '22 at 18:07
  • Is this always first name and last name or does this string contain middle names? – jabaa Jun 30 '22 at 18:13
  • You can not get what you want with your logic. What about a person with a middle name? I do not think this is even a python question. – bekirbakar Jun 30 '22 at 18:41

3 Answers3

0

Here is another solution, it uses more_itertools to simplify a little bit. It's yield slices of length n from the sequence seq.

from more_itertools import sliced      # import this lib first

# your names:  

outs = [' '.join(n) for x in sliced(names.split(), 2) ]

print(outs)

Output:

['John Doe', 'Jane Doe', 'John Smith', 'Jane Smith']
Daniel Hao
  • 4,922
  • 3
  • 10
  • 23
0

Consider using regular expressions to solve this.

>>> names = "John Doe Jane Doe John Smith Jane Smith"
>>> re.findall(r'\b[a-zA-Z]+\s+[a-zA-Z]+\b', names)
['John Doe', 'Jane Doe', 'John Smith', 'Jane Smith']
>>>
Chris
  • 26,361
  • 5
  • 21
  • 42
  • 1
    Like regex, it could be put this way too: `re.findall(r'\b[\w]+\s+[\w]+\b', names)` But think it could be little challenging for the beginners.. – Daniel Hao Jul 01 '22 at 10:01
-1

Alternatively, and without imports, you could use a list comprehension, which is very pythonic

>>> [' '.join(x.split()[i:i+2]) for i in range(0,len(x.split()),2)] #list comprehension
['Jane Doe', 'John Doe']

If you only need to iterate over it once, a generator may be more suitable

>>> x="Jane Doe John Doe"
>>> print(' '.join(x.split()[i:i+2]) for i in range(0,len(x.split()),2)) #generator object
>>> for i in (' '.join(x.split()[i:i+2])for i in range(0,len(x.split()),2)): #iterating through the generator
    print(i)

Jane Doe
John Doe
<generator object <genexpr> at 0x0000000000000000>

If you want to perform other transformations on the data, python has set comprehensions and dictionary comprehensions available.

>>> y='John Doe Jane Doe John Doe'
>>> {' '.join(y.split()[i:i+2]) for i in range(0,len(y.split()),2)}
{'John Doe', 'Jane Doe'} #set comprehension (to remove duplicates)
>>> {i//2:' '.join(x.split()[i:i+2]) for i in range(0,len(x.split()),2)} #dictionary comprehension (keys arbitrary)
{0: 'Jane Doe', 1: 'John Doe'}
Mous
  • 953
  • 3
  • 14