Reputation: 213
I have a list like
[(1, 3), (6, 7)]
and a string
'AABBCCDD'
I need to get the result AABCD
.
I know I can get the integers form the tuple with nameOfTuple[0][0]
yielding 1.
I also know that I can get the chars form the string with nameOfString[0]
yielding A.
My question is, how do I iterate through two arguments in the tuple, in order to save the integers (to a list maybe) and then get the chars from the string?
Upvotes: 2
Views: 1472
Reputation: 500277
In [1]: l = [(1, 3), (6, 7)]
In [2]: s = 'AABBCCDD'
In [3]: ''.join(s[start-1:end] for (start,end) in l)
Out[3]: 'AABCD'
Here, pairs of indices from l
are assigned to start
and end
, one pair at a time. The relevant portion of the string is then extracted using s[start-1:end]
, yielding a sequence of strings. The strings are then merged using join()
.
Upvotes: 10