【发布时间】:2022-01-25 01:52:05
【问题描述】:
所以我在这里尝试优化我拥有的一些代码。
代码检查任何列表是否为空,如果不是,则链接到该列表的相应文件夹将保存在外部程序中,因此为了更清楚,这是代码:
if len(match_FIRST)>0 and len(match_SECOND)>0 and len(match_THIRD)>0 and len(match_FOURTH)>0:
keep = ['FIRST', 'SECOND', 'THIRD', 'FOURTH']
elif len(match_FIRST)>0 and len(match_SECOND)>0 and len(match_THIRD)>0 and len(match_FOURTH)==0:
keep = ['FIRST', 'SECOND', 'THIRD']
elif len(match_FIRST)>0 and len(match_SECOND)>0 and len(match_THIRD)==0 and len(match_FOURTH)>0:
keep = ['FIRST', 'SECOND', 'FOURTH']
elif len(match_FIRST)>0 and len(match_SECOND)==0 and len(match_THIRD)>0 and len(match_FOURTH)>0:
keep = ['FIRST', 'THIRD', 'FOURTH']
elif len(match_FIRST)>0 and len(match_SECOND)>0 and len(match_THIRD)==0 and len(match_FOURTH)==0:
keep = ['FIRST', 'SECOND']
elif len(match_FIRST)>0 and len(match_SECOND)==0 and len(match_THIRD)>0 and len(match_FOURTH)==0:
keep = ['FIRST', 'THIRD']
... etc
所以基本上我们将以这种方式保留在 keep= [] 中仅列出链接到非空列表的名称...
你有什么建议可以优化这个的算法,因为现在我只有四个列表,用这种方式手动检查很容易......但需要更好的解决方案......
请指教!
谢谢,
敬礼
【问题讨论】:
-
这能回答你的问题吗? How to remove empty lists in python
标签: python list optimization