10

I have a list of sentences like:

lst = ['A B C D','E F G H I J','K L M N']

What i did is

l = []
for i in lst:
    for j in i.split():
        print(j)
        l.append(j) 

first = l[::2]
second = l[1::2]

[m+' '+str(n) for m,n in zip(first,second)]

The Output i got is

lst = ['A B', 'C D', 'E F', 'G H', 'I J', 'K L', 'M N']

The Output i want is:

lst = ['A B', 'B C','C D','E F','F G','G H','H I','I J','K L','L M','M N']

I am struggling to think how to achieve this.

3
  • [i for x in map(lambda x: x.split(' '), lst) for i in map(' '.join, zip(x[:-1], x[1:]))] Commented Jul 23, 2019 at 15:43
  • Did you try my answer? It's shorter than the accepted one Commented Jul 23, 2019 at 20:09
  • Your solution is already correct if you replace ::2 with : in both places Commented Jul 24, 2019 at 16:56

4 Answers 4

11

First format your list of string into a list of list, then do a mapping by zip.

i = [i.split() for i in lst]

f = [f"{x} {y}" for item in i for x,y in zip(item,item[1::])]

print (f)

#['A B', 'B C', 'C D', 'E F', 'F G', 'G H', 'H I', 'I J', 'K L', 'L M', 'M N']
Sign up to request clarification or add additional context in comments.

5 Comments

+1. Can also condense into a one-liner if you replace i with the first list comprehension itself (current code is more readable though)
the [x for x in i.split(" ")] could be just i.split()... Nice solution +1
@Tomerikoo You are right. Force of habit to always write what I split on :)
I was referring to the whole list comprehension as split already returns a list and not a generator or something... I believe it makes it a bit more readable
Cool, if I could, would be +2 :)
3

Your problem is that you're flattening the whole list and dividing to couples when you want to divide to subsequent couples only the inner elements. So for that we will perform the operation on each element separatly:

lst = ['A B C D','E F G H I J','K L M N']

res = []
for s in lst:
    sub_l = s.split()
    for i in range(len(sub_l)-1):
        res.append("{} {}".format(sub_l[i], sub_l[i+1]))
print(res)

Gives:

['A B', 'B C', 'C D', 'E F', 'F G', 'G H', 'H I', 'I J', 'K L', 'L M', 'M N']

Comments

2
nested = []
for item in lst:
    item = (' '.join(item).split())
    for ix in range(len(item) - 1):
        nested.append(' '.join(item[ix:ix + 2]))

print (nested)

output:

['A B', 'B C', 'C D', 'E F', 'F G', 'G H', 'H I', 'I J', 'K L', 'L M', 'M N']

1 Comment

@Tomerikoo is correct, the output is slightly different, I was doing the same as @ncica. You'll probably need to add some if condition to not append a few elements
1

Here is a method using Regex:

import re
lst = ['A B C D','E F G H I J','K L M N']
result = re.findall(r'(?=(\b\w+?\b \b\w+?\b))', str(lst))
print(result)

Output:

['A B', 'B C', 'C D', 'E F', 'F G', 'G H', 'H I', 'I J', 'K L', 'L M', 'M N']

2 Comments

Thnks for the answer...Yes i have already tried it and its not working it gives only the last and first value of both words....every words in list is actual word not an alphabet.
@jamesjoyce I've edited it so now it will work for words. And please be more specific in your questions in future because you didn't mention that it would be words and not just letters.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.