简体   繁体   中英

Iterate through tuple values python

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?

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM