如何从一个有条件的主列表中生成两个列表

2024-06-09 00:34:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下意见:

my_list = [
    0, 'root', 'disable', 'dynamic', 'pages', 'login', 'sensitive', 'registration', 'screen', 'flag_secure', 'application', 'frida', 'blurring', 'instrumentation', 'mechanism', 'recent', 'debugger', 'information', 'restrict', 'stronger', 'anti', 'capture', 'appsview', 'strengthen', 'code', 'data', 'exported', 'use', 'protection', 'default', 'ensure', 'hide', 'improve', 'runtime', 'copy', 'paste', 'expired', 'impelement', 'frequency', 'adding', 'feature', 'instrument', 'attempts', 'avoid', 'escape', 'authentication', 'encryption', 'components', 'device', 'char', 'verification', 
    1, 'detection', 'frequency', 'dynamic', 'emulator', 'encoding', 'encrypt', 'encryption', 'ensure', 'escape', 'expired', 'exported', 'feature', 'flag_secure', 'frame', 'frida', 'device', 'generated', 'hardcoding', 'headers', 'hide', 'impelement', 'implement', 'improve', 'information', 'input', 'instrument', 'instrumentation', 'integrity', 'disable', 'version', 'verification', 'char', 'access', 'adding', 'alert', 'anti', 'application', 'appsview', 'array', 'attempts', 'authentication', 'avoid', 'blurring', 'capture', 'character', 'default', 'check', 'code', 'come', 'components', 
    2, 'implement', 'version', 'frequency', 'dynamic', 'emulator', 'encoding', 'encrypt', 'encryption', 'ensure', 'escape', 'expired', 'exported', 'feature', 'flag_secure', 'frame', 'frida', 'device', 'generated', 'hardcoding', 'headers', 'hide', 'impelement', 'improve', 'information', 'input', 'instrument', 'instrumentation', 'integrity', 'disable', 'detection', 'verification', 'char', 'access', 'adding', 'alert', 'anti', 'application', 'appsview', 'array', 'attempts', 'authentication', 'avoid', 'blurring', 'capture', 'character', 'default', 'check', 'code', 'come', 'components', 
    3, 'access', 'version', 'frequency', 'dynamic', 'emulator', 'encoding', 'encrypt', 'encryption', 'ensure', 'escape', 'expired', 'exported', 'feature', 'flag_secure', 'frame', 'frida', 'device', 'generated', 'hardcoding', 'headers', 'hide', 'impelement', 'implement', 'improve', 'information', 'input', 'instrument', 'instrumentation', 'integrity', 'disable', 'detection', 'verification', 'default', 'adding', 'alert', 'anti', 'application', 'appsview', 'array', 'attempts', 'authentication', 'avoid', 'blurring', 'capture', 'char', 'character', 'check', 'code', 'come', 'components'
]

从这个列表中,如何为列表中的每个整数创建两个新列表,并在每个新列表中追加string元素

例如,新列表应包含第一个整数(即0或1或2或3),以及列表中下一个整数之前的十几个字符串元素

这就是我希望得到的结果:

List0 = [0, 'root', 'disable', 'dynamic', 'pages', 'login', 'sensitive', 'registration', 'screen', 'flag_secure', 'application', 'frida', 'blurring', 'instrumentation', 'mechanism', 'recent', 'debugger', 'information', 'restrict', 'stronger', 'anti', 'capture', 'appsview', 'strengthen', 'code', 'data', 'exported', 'use', 'protection', 'default', 'ensure', 'hide', 'improve', 'runtime', 'copy', 'paste', 'expired', 'impelement', 'frequency', 'adding', 'feature', 'instrument', 'attempts', 'avoid', 'escape', 'authentication', 'encryption', 'components', 'device', 'char', 'verification']`

List1 = [1, 'detection', 'frequency', 'dynamic', 'emulator', 'encoding', 'encrypt', 'encryption', 'ensure', 'escape', 'expired', 'exported', 'feature', 'flag_secure', 'frame', 'frida', 'device', 'generated', 'hardcoding', 'headers', 'hide', 'impelement', 'implement', 'improve', 'information', 'input', 'instrument', 'instrumentation', 'integrity', 'disable', 'version', 'verification', 'char', 'access', 'adding', 'alert', 'anti', 'application', 'appsview', 'array', 'attempts', 'authentication', 'avoid', 'blurring', 'capture', 'character', 'default', 'check', 'code', 'come', 'components']`

# ...etc

我怎样才能做到这一点


Tags: defaultinformationapplicationcodedynamiccaptureflagsecure
2条回答

您可以找到列表中出现整数的索引。在列表之外添加“索引”,即列表的长度。这些代表了从你需要的块中取出的切入点

使用zip可以组合这些块的开始/结束范围,并使用这些范围相应地对列表进行切片:

cuts = [i for i, val in enumerate(my_list) if isinstance(val, int)] + [len(my_list)]
result = [my_list[i:j] for i, j in zip(cuts, cuts[1:])]

结果将是一个列表列表。您可以按如下方式打印每个列表:

for lst in result:
    print(lst)

如果您希望一个列表中包含十几个列表:

def iter_and_pack_by_number(lst):
    answer = []
    values = []
    for index in range(len(lst)):
        if (isinstance(lst[index], int)) and index != 0:
            answer.append(values)
            values = []
        values.append(lst[index])
    
    return answer

result

相关问题 更多 >