2

I have tried the Sequence Generator like Lambda, List comprehension and others but it seems that I am not able to get what I really want. My final goal is to print sequence of words from a string like string[1:3]

What I am looking for :

a = [0,13,26,39]
b = [12,25,38,51]

str = 'If you are done with the file, move to the command area across from the file name in the RL screen and type'

read = str.split()

read[0:12]
['If', 'you', 'are', 'done', 'with', 'the', 'file,', 'move', 'to', 'the', 'command', 'area']
read[13:25]
['from', 'the', 'file', 'name', 'in', 'the', 'RL', 'screen', 'and', 'type']
2
  • 2
    Well, what's the question? Commented Jun 21, 2013 at 16:57
  • If I may ask, in what context did you find such a problem? Commented Jun 21, 2013 at 18:29

4 Answers 4

5

Use zip:

>>> a = [0,13,26,39]
>>> b = [12,25,38,51]
>>> strs = 'If you are done with the file, move to the command area across from the file name in the RL screen and type'
>>> spl = strs.split()
>>> for x,y in zip(a,b):
...     print spl[x:y]
...     
['If', 'you', 'are', 'done', 'with', 'the', 'file,', 'move', 'to', 'the', 'command', 'area']
['from', 'the', 'file', 'name', 'in', 'the', 'RL', 'screen', 'and', 'type']
[]
[]

zip returns list of tuples, where each tuple contains items on the same index from the iterables passed to it:

>>> zip(a,b)
[(0, 12), (13, 25), (26, 38), (39, 51)]

Use itertools.izip if you want memory efficient solution, as it returns an iterator.

You can use str.join if you want to create a string from that sliced list:

for x,y in zip(a,b):
    print " ".join(spl[x:y])
...     
If you are done with the file, move to the command area
from the file name in the RL screen and type

Update: Creating a and b:

>>> n = 5
>>> a = range(0, 13*n, 13)
>>> b = [ x + 12 for x in a]
>>> a
[0, 13, 26, 39, 52]
>>> b
[12, 25, 38, 51, 64]
Sign up to request clarification or add additional context in comments.

1 Comment

And how can I create the list a and b. These are sequences in which N+1 number is 13 more in case of a and b. Also b[0] = a[0] + 12
3

Do you mean:

>>> [read[i:j] for i, j in zip(a,b)]
[['If', 'you', 'are', 'done', 'with', 'the', 'file,', 'move', 'to', 'the', 
'command',    'area'], ['from', 'the', 'file', 'name', 'in', 'the', 'RL',
'screen', 'and', 'type'], [], []]

or

>>> ' '.join[read[i:j] for i, j in zip(a,b)][0])
'If you are done with the file, move to the command area'

>>> ' '.join[read[i:j] for i, j in zip(a,b)][1])
'from the file name in the RL screen and type'

Comments

2
a = [0,13,26,39]
b = [12,25,38,51]
str = 'If you are done with the file, move to the command area across from the file name  in the RL screen and type'

read = str.split()
extra_lists = [read[start:end] for start,end in zip(a,b)]
print extra_lists

Comments

1

You mentioned a lambda, so:

 f = lambda s, i, j: s.split()[i:j]
 >>> f("hello world how are you",0,2)
 ['hello', 'world']

Seems like you're doing the slice indices in two lists, might I suggest a dictionary or a list of tuples?

str = 'If you are done with the file, move to the command area across from the file name in the RL screen and type'
slices = [(0, 13), (12, 25)]
dslices = {0:13, 12:25}
for pair in slices:
    print f(str, pair[0], pair[1])
for key in dslices:
    print f(str, key, dislikes[key])

I'm not a fan of using zip when you have the option of just formatting your data better.

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.