温馨提示:本文翻译自stackoverflow.com,查看原文请点击:python - Alphabetize and then split on character in nested list

python - 按字母顺序排列,然后按嵌套列表中的字符拆分

发布于 2020-03-27 11:56:52

我有一些继承的代码可以在其他情况下使用,但不能在当前应用程序中使用...

我有以下输入:

import itertools

split_into_tables = [['abc=0', 'def=0', 'imsi=0', 'kekri=1234567890', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9'], ['abc=0', 'def=0', 'imsi=0', 'kekri=NA', 'sum=asdf', 'origin=source', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9'], ['addkik=BasdferySaver', 'addpop=false', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=device_id', 'addpop=F309387C-AAF5-478D-95A2-28E9B46105C7', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=tmgfd', 'addpop=fixed_ap', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=jej_unified', 'addpop=false', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=internalversion', 'addpop=19.1.0.12', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=version', 'addpop=3.11.0', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=carrierid', 'addpop=AT&T', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=activecarrierid', 'addpop=0', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=UserConsent', 'addpop=yes', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=idk', 'addpop=2019-07-02T14:28:48+0000', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT'], ['addkik=ieir', 'addpop=sdfhs', 'sum=asdf', 'origin=d1', 'sID=FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'gjk=EDT']]

下面的代码应基于'='之前的第一个字符串按字母顺序排列每个嵌套列表,然后再将其拆分为2个字符串('='之前的字符串和之后的字符串)。

e=0
while e < len(split_into_tables):
    if split_into_tables[e] == ['No result found=No result found']:
        e = e+1
    else:
        si = iter(split_into_tables[e])
        split_into_tables[e] = ['='.join(each) for each in itertools.izip(si, si)]
        e = e+1
e_ = 0
while e_ < len(split_into_tables):
    split_into_tables[e_] = sorted(split_into_tables[e_], key=str.lower)
    e_ = e_+1

split_into_tables_alpha = [sum([x.split('=') for x in sublist], []) for sublist in split_into_tables]

但这是我得到的输出:

split_into_tables_alpha = [['abc', '0', 'def', '0', 'imsi', '0', 'kekri', '1234567890', 'sum', 'asdf', 'origin', 'd1'], ['abc', '0', 'def', '0', 'imsi', '0', 'kekri', 'NA', 'origin', 'source', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9'], ['addkik', 'BasdferySaver', 'addpop', 'false', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'device_id', 'addpop', 'F309387C-AAF5-478D-95A2-28E9B46105C7', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'tmgfd', 'addpop', 'fixed_ap', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'jej_unified', 'addpop', 'false', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'internalversion', 'addpop', '19.1.0.12', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'version', 'addpop', '3.11.0', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'carrierid', 'addpop', 'AT&T', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'activecarrierid', 'addpop', '0', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'UserConsent', 'addpop', 'yes', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'idk', 'addpop', '2019-07-02T14:28:48+0000', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf'], ['addkik', 'ieir', 'addpop', 'sdfhs', 'gjk', 'EDT', 'origin', 'd1', 'sID', 'FD330AD9-1FEB-4A33-BC0F-CF760AE203E9', 'sum', 'asdf']]

而且此输出缺少原始列表中的某些元素,但它始终是嵌套列表中的最后一个元素(如果实际上删除了一个值)。例如,在第一个嵌套列表中,“ sID = FD330AD9-1FEB-4A33-BC0F-CF760AE203E9”应细分为“ sID”和“ FD330AD9-1FEB-4A33-BC0F-CF760AE203E9”(按字母顺序排列,会最后出现)在输出中),但已完全删除...

有任何想法吗?

查看更多

查看更多

提问者
cjg123
被浏览
57
Deepstop 2019-07-04 00:24

这包括等号后的值。Itertools.chain(..)在通过split函数创建2个元素列表之后,将列表变平。

[list(itertools.chain(*sorted([inner.split("=") for inner in outer]))) for outer in split_into_tables]