简体   繁体   English

扩展清单清单

[英]Expanding list of lists

I am looking for a way to optimize the current code that I have: 我正在寻找一种优化现有代码的方法:

        for result in results:
            if result != '':
                if isinstance(result, list):
                    for subresult in result:
                        if subresult != '':
                            worksheet.write(r, c, subresult)
                            c += 1
                else:
                    worksheet.write(r, c, result)
                    c += 1

I also use this in a particular case: 在特殊情况下,我也会使用它:

        for result in results:
            if isinstance(result, list):
                for subresult in result:
                    worksheet.write(r, c, subresult)
                    c += 1
            else:
                worksheet.write(r, c, result)
                c += 1

I have a list that in some instances contains only a single value, while in other instances it contains multiple values (ie another list). 我有一个列表,在某些情况下仅包含一个值,而在其他情况下则包含多个值(即另一个列表)。

Here is an example of what the list may contain: 这是列表可能包含的内容的一个示例:

results = ['', '', '', '390', '66', ['Name', 'SerialNumber', 'Model', 'Year'], 'SW_Version', ['HD_Loc', 'HD_Model', 'HD_FW', 'HD_SerialNumber', 'Man_Yr', 'Man_Mth'], '', '', '']

Ultimately I want to be able to output each value to its own column in excel. 最终,我希望能够将每个值输出到excel中自己的列。 In some cases I only want to output the parts of the list that contain a value, which is performed by the first snippet of code I provided. 在某些情况下,我只想输出列表中包含一个值的部分,这是由我提供的第一段代码执行的。 In other cases I want to output all parts of the list, regardless of whether it has a value or not, which is performed by the second snippet of code I provided. 在其他情况下,我想输出列表的所有部分,无论它是否具有值,这都是由我提供的第二段代码执行的。

I have seen instances where a for loop and some action statement, lets say print , have been combined into a single line of python code, but I have been unable to get that syntax to work with the snippets that I have provided. 我见过一些实例,其中有一个for循环和一些动作语句(比如说print )被组合成一行python代码,但是我无法使该语法与我提供的代码段一起使用。

This code is currently working in the way I have implemented it, I am just looking for a way to shorten/optimize it as the program I have it inside of is starting to get fairly large and being able to shorten this would help me in shortening other parts of the program also. 该代码当前以我实现它的方式起作用,我只是在寻找一种缩短/优化它的方法,因为我拥有它的程序开始变得相当大,并且能够缩短它可以帮助我缩短程序的其他部分也是如此。

Thanks in advance! 提前致谢!

Edit: In both cases the order of which it is output does matter, and I would like to have it output in the order that it is currently in (just minus the '' in the one scenario). 编辑:在两种情况下,它的输出顺序都很重要,我希望按当前的顺序输出它(在一种情况下仅减去'' )。

You can do this neatly with recursion: 您可以通过递归巧妙地做到这一点:

def worksheet_writer(results):
        if isinstance(results, list):
                for item in results: worksheet_writer(item)
        else:
                worksheet.write(r, c, results)
                c += 1

Edited to support strings. 编辑以支持字符串。

Okay here's the solution I've got so far. 好的,这是我到目前为止的解决方案。 It's a kinda messy list comprehension. 这是一个有点凌乱的列表理解。 I'll try and clean it up a bit. 我会尽力清理一下。

results = ['', '', '', '390', '66', ['Name', 'SerialNumber', 'Model', 'Year'], 'SW_Version', ['HD_Loc', 'HD_Model', 'HD_FW', 'HD_SerialNumber', 'Man_Yr', 'Man_Mth'], '', '', '']
li = list(set([item if type(sublist) is list else sublist for sublist in results for item in sublist]))
print li

This gives an output of: 输出为:

['SW_Version', 'Name', 'HD_FW', 'SerialNumber', 'HD_Model', 'HD_SerialNumber', '390', '66', 'Year', 'Model', 'Man_Yr', 'HD_Loc', 'Man_Mth']

I hope this helps, 我希望这有帮助,

results = ['', '', '', '390', '66', ['Name', 'SerialNumber', 'Model', 'Year'], 'SW_Version', ['HD_Loc', 'HD_Model', 'HD_FW', 'HD_SerialNumber', 'Man_Yr', 'Man_Mth'], '', '', '']

flatList = [item for sublist in [[item] if not isinstance(item,list) else item for item in results] for item in sublist]

flatListWithoutBlanks = [item for item in [item for sublist in [[item] if not isinstance(item,list) else item for item in results] for item in sublist] if item != '' ]

best, 最好,

我将其发布在评论中,但不确定是否会清楚,

flatListWithoutBlanks = [item for item in [item for sublist in [[item] if not isinstance(item,list) else item for item in results] for item in sublist] if item != '' ]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM