你能把Try&条款理解除了条款或实施替代?

时间:2014-02-17 00:26:39

标签: python list list-comprehension sublist

如何从遵循此逻辑的不同子列表列表中创建子列表列表:

  • 如果子列表中的项目是子列表本身,则仅拉取第二个第三项并将其转换为字符串。我所说的'子列表中的项目是子列表本身'是指在子列表中的子列表中有子列表的情况。

  • 如果子列表中的项目不是子列表,只需将其转换为字符串

原始列表:

 [['24000.0', [item1,item2,'Bobby Fish']], ['24000.0', [item1,item2,'Abraham Lincoln']], ['9000.0', [item1,item2,'Abraham Lincoln']], ['12000.0', [item1,item2,'George Washington']], ['12000.0', [item1,item2,'George Washington']], ['6000.0', [item1,item2,'Abraham Lincoln']], ['15000.0', [item1,item2,'Abraham Lincoln']], ['15000.0', [item1,item2,'George Washington']], ['12000.0', [item1,item2,'Bobby Fish']], ['3000.0', [item1,item2,'Bobby Fish']], ['600.0', [item1,item2,'Bobby Fish']], ['1800.0', [item1,item2,'Bobby Fish']], ['6600.0', [item1,item2,'Abraham Lincoln']], ['11000.0', [item1,item2,'Bobby Fish']], ['8000.0', [item1,item2,'Abraham Lincoln']], ['4000.0', [item1,item2,'Abraham Lincoln']], ['12000.0', [item1,item2,'Abraham Lincoln']], ['2500.0', [item1,item2,'George Washington']], ['3000.0', [item1,item2,'Abraham Lincoln']], ['20000.0', [item1,item2,'George Washington']], ['12000.0', [item1,item2,'George Washington']], ['12000.0', [item1,item2,'George Washington']], ['3000.0', [item1,item2,'George Washington']]]

所需清单:

  [['24000.0', 'Bobby Fish'], ['24000.0', 'Abraham Lincoln'], ['9000.0', 'Abraham Lincoln'], ['12000.0', 'George Washington'], ['12000.0', 'George Washington'], ['6000.0', 'Abraham Lincoln'], ['15000.0', 'Abraham Lincoln'], ['15000.0', 'George Washington'], ['12000.0', 'Bobby Fish'], ['3000.0', 'Bobby Fish'], ['600.0', 'Bobby Fish'], ['1800.0', 'Bobby Fish'], ['6600.0', 'Abraham Lincoln'], ['11000.0', 'Bobby Fish'], ['8000.0', 'Abraham Lincoln'], ['4000.0', 'Abraham Lincoln'], ['12000.0', 'Abraham Lincoln'], ['2500.0', 'George Washington'], ['3000.0', 'Abraham Lincoln'], ['20000.0', 'George Washington'], ['12000.0', 'George Washington'], ['12000.0', 'George Washington'], ['3000.0', 'George Washington']]

最初,我有这个代码,它做了我想要的,但是当项目是子列表时,这不起作用,因为我只想要那些情况下的第二个项目。

lst = [[str(subli[i]) for i in range(2,len(subli))] for subli in qa[sf.records:]]

我想出了我想要的代码,但它的速度是列表理解的两倍,所以我想知道是否有办法让这个列表理解,或者无论如何使它更快。问题是在列表解析中实现try和except语句

    Lst = []
    for subli in qa[sf.records:]:
        Sublist = []
        for item in subli[2:]:
            try:
                Sublist.append(str(item[2]))
            except IndexError:
                Sublist.append(str(item))
        Lst.append(Sublist)
    print Lst

产生:

[['24000.0', 'Bobby Fish'], ['24000.0', 'Abraham Lincoln'], ['9000.0', 'Abraham Lincoln'], ['12000.0', 'George Washington'], ['12000.0', 'George Washington'], ['6000.0', 'Abraham Lincoln'], ['15000.0', 'Abraham Lincoln'], ['15000.0', 'George Washington'], ['12000.0', 'Bobby Fish'], ['3000.0', 'Bobby Fish'], ['600.0', 'Bobby Fish'], ['1800.0', 'Bobby Fish'], ['6600.0', 'Abraham Lincoln'], ['11000.0', 'Bobby Fish'], ['8000.0', 'Abraham Lincoln'], ['4000.0', 'Abraham Lincoln'], ['12000.0', 'Abraham Lincoln'], ['2500.0', 'George Washington'], ['3000.0', 'Abraham Lincoln'], ['20000.0', 'George Washington'], ['12000.0', 'George Washington'], ['12000.0', 'George Washington'], ['3000.0', 'George Washington']]

我很感激上述代码的任何速度改进。

1 个答案:

答案 0 :(得分:1)

如果每个子列表的格式始终为[numberstring, [item1, item2, string]],那么这应该有效:

lst = [ [ str(subli[0]), subli[1][2] ] for subli in qa[sf.records:] ]

如果子列表是任意长的并且其中包含任何一种项目,则应修改您的代码以仅测试子列表项是否为列表(并查看所有项目),例如:

Lst = []
for subli in qa[sf.records:]:
    Sublist = []
    for item in subli:
        if isinstance( item, list ):
            Sublist.append( str(item[2]) )
        else:
            Sublist.append( str(item) )
    Lst.append(Sublist)

当采用异常路径时,try / except将花费大量时间;测试更快。