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

Alphabetize and then split on character in nested list

提问人:cjg123 提问时间:7/3/2019 最后编辑:Deepstopcjg123 更新时间:7/4/2019 访问量:30

问:

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

我有以下输入:

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”(按字母顺序排列,它们将出现在输出的最后),但它被完全删除了......

有什么想法吗?

python python-2.7 嵌套列表

评论


答:

1赞 shaik moeed 7/3/2019 #1
>>> 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']]

输出:

    >>> sorted([[ei.split("=")[0] for ei in e ]for e in split_into_tables ])

    [['abc', 'def', 'imsi', 'kekri', 'sum', 'origin', 'sID'], ['abc', 'def', 
'imsi', 'kekri', 'sum', 'origin', 'sID'], ['addkik', 'addpop', 'sum', 'origin', 
'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 'sID', 'gjk'], ['addkik', 
'addpop', 'sum', 'origin', 'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 
'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 'sID', 'gjk'], ['addkik', 
'addpop', 'sum', 'origin', 'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 
'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 'sID', 'gjk'], ['addkik', 
'addpop', 'sum', 'origin', 'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 
'sID', 'gjk'], ['addkik', 'addpop', 'sum', 'origin', 'sID', 'gjk']]

评论

0赞 cjg123 7/3/2019
我仍然需要嵌套结构...“=”之后出现的值在哪里?
0赞 shaik moeed 7/3/2019
@cjg123 立即检查,以及您希望在 .?您可以添加预期的输出样本吗?=
0赞 cjg123 7/3/2019
有一个名为“split_into_tables_alpha”的示例,但不要从中删除任何值。但是,是的,我现在会这样做......
1赞 cjg123 7/3/2019
谢谢你的帮助!
1赞 Deepstop 7/3/2019 #2

这包括等号后面的值。 从 split 函数制作 2 个元素列表后展平列表。Itertools.chain(..)

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

评论

0赞 cjg123 7/3/2019
了不起!谢谢!