0

我有 2 个列表:其中 1 个是列表列表。它们如下-

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

just_a_list =['THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG', 'IWOULDLOVETOGETOVERWITHTHISASSOONASPOSSIBLE']

lists_of_lists 用于对 just_a_list 的元素进行切片,这样: ['1', '4', '7', '13', '16', '21', '32', '36']将字符串切片'THEQUICKBROWNFOXJUMPEDOVERTHELAZYDOG'如下

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

需要注意的地方——

中的每个列表list_of_lists都有偶数个数字。

i'thin 位置的列表list_of_lists将属于 in 位置的i'th字符串just_a_list

请帮助我了解如何执行上述过程..

谢谢

4

2 回答 2

5

用于zip()组合字符串和切片列表,然后使用zip()加号iter()技巧来配对起始值和终止值:

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])

请注意,我们必须1 添加到停止索引,因为您似乎需要它是包容性的,而在 Python 中,停止索引是独占的。

这给出了:

>>> 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
于 2013-03-25T16:50:44.950 回答
0

如果我理解正确:

>>> 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'
于 2013-03-25T16:51:23.943 回答