0

I have 2 lists: 1 of which is a lists of lists. They are as follows-

lists_of_lists = ['1', '4', '7', '13', '16', '21', '32', '36'],['3', '6', '8', '14', '22', '26', '31', '40']

just_a_list =['THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG', 'IWOULDLOVETOGETOVERWITHTHISASSOONASPOSSIBLE']

The lists_of_lists are used for slicing the elements of just_a_list such that: ['1', '4', '7', '13', '16', '21', '32', '36'] would slice the string 'THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG' as follows

'1' - '4' - 'HEQU'
'7' - '13' - 'KBROWNF'
'16' - '21' - 'JUMPED'
'32' - '36' - 'ZYDOG'

points to note-

Each list in list_of_lists will have an even number of numbers.

The list at i'th position in list_of_lists will belong to the string present at the i'th position in just_a_list.

Please help me out as to how do I carry out the process described above..

Thanks

femtoRgon
  • 32,893
  • 7
  • 60
  • 87
begin.py
  • 161
  • 1
  • 1
  • 9

2 Answers2

5

Use zip() to combine the string and slice lists, then use a zip() plus iter() trick to pair the start and stop values:

for slicelist, text in zip(lists_of_lists, just_a_list):
    for start, stop in zip(*([iter(slicelist)]*2)):
        print(text[int(start):int(stop) + 1])

Note that we have to add 1 to the stop index, as your appear to need it to be inclusive, while in Python the stop index is exclusive.

This gives:

>>> for slicelist, text in zip(lists_of_lists, just_a_list):
...     for start, stop in zip(*([iter(slicelist)]*2)):
...         print(text[int(start):int(stop) + 1])
... 
HEQU
KBROWNF
JUMPED
YDOG
ULDL
VETOGET
HTHIS
ONASPOSSIB
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 1
    @julio.alegria: Note that I didn't explain how it all works in detail. If I was the teacher, I'd ask specific questions as to how the `zip(*([iter(slicelist)]*2))` part works.. – Martijn Pieters Mar 25 '13 at 17:01
  • This is so EPIC. Could you please explain it to me as to how the code works. i have no idea as to whats going on in there. – begin.py Mar 25 '13 at 17:02
  • 2
    @begin.py: Ah, but there's the rub, innit. :-) I explained how *most* of it works. You can find the functions I used in the [Python functions documentation](http://docs.python.org/2/library/functions.html). – Martijn Pieters Mar 25 '13 at 17:03
  • 1
    @begin.py: The `zip()` + `iter()` stanza has been discussed here on Stack Overflow before, specifically in [Iterating over every two elements in a list](http://stackoverflow.com/q/5389507) – Martijn Pieters Mar 25 '13 at 17:09
  • @julio.alegria - I would love to show you the file i am working on. The code given here, will help me for just about 10% of my work.Once upon a time even you were a learner, even you needed help. Please dont comment if you cannot be constructive or helpfull. – begin.py Mar 25 '13 at 17:13
  • @begin.py Of course I was (and still am) a learner, that's why I don't like these "do-my-homework" kind of questions, they don't help you to (guess what?) *learn*. – juliomalegria Mar 25 '13 at 17:42
0

If I understand you right:

>>> ls = just_a_list =['THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG', 'IWOULDLOVETOGETOVERWITHTHISASSOONASPOSSIBLE']

>>> ls[0]
'THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG'

# so we do    
# your index was off by one
>>> ls[0][1:5]
'HEQU'

>>> ls[0][7:14]
'KBROWNF'
ninMonkey
  • 7,211
  • 8
  • 37
  • 66