【问题标题】:How do I use list comprehension to simplify the sorting and adding of multiple lists and strings?如何使用列表推导来简化多个列表和字符串的排序和添加?
【发布时间】:2018-10-09 09:48:02
【问题描述】:

下面的循环将获取目录和/或图像列表,并将返回图像列表,包括提供的目录中的图像。对于给定的每个目录,它会验证每个文件确实是一个有效的图像,然后再将该图像添加到列表中。

import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("-style_image", help="List of images and/or directories")
params = parser.parse_args()

style_image_input = params.style_image.split(',')
valid_ext, style_input_sorted = [".jpg",".png"], None
for image in style_image_input:
    if os.path.isdir(image):
        for file in os.listdir(image):
            print(file)
            ext = os.path.splitext(file)[1]
            if ext.lower() not in valid_ext:
                continue
            if style_input_sorted == None:
                style_input_sorted = file
            else: 
                style_input_sorted += "," + file
    else:
        if style_input_sorted == None:
            style_input_sorted = image
        else: 
            style_input_sorted += "," + image 
style_image_list = style_input_sorted.split(',')
print(style_image_list)

如何使用列表推导来简化这个循环?

【问题讨论】:

  • 你不能。为什么你会考虑将一个 16 行长的循环变成一个列表理解?
  • 因为重复的模式似乎可以让我简化代码,从而减少行数。

标签: python string sorting directory list-comprehension


【解决方案1】:

忘记“我怎样才能把它变成一个列表理解”。从“我怎样才能简化这个”开始。如果到最后,您进入一个包含一两个子句和一个简单表达式的循环,那么您可以考虑将其转换为列表推导式,但这是最后一步,而不是开始目标。


你的大部分重复都是在你建立style_input_sorted的方式上:

  • 将其设置为None
  • 每次获取一个值,如果是None,就设置成这个值
  • 否则,添加逗号,然后添加值。

可以"" 开头,而不是以None 开头,然后执行以下操作:

if style_input_sorted:
    style_input_sorted += ","
style_input_sorted += file

但是,更简单地说:您正在做的是同一件事str.join 已经知道该怎么做。如果你可以建立一个 list 字符串,然后在最后只用join 那个列表,那就简单多了:

style_input_sorted = []
if …
    for …
        style_input_sorted.append(file)
else …
    style_input_sorted.append(file)
style_input_sorted = ",".join(style_input_sorted)

但看起来你对style_input_sorted 所做的唯一事情就是将它拆分回一个列表中。那么,为什么还要加入一个字符串来分割它呢?

style_input_list = []
if …
    for …
        style_input_list.append(file)
else …
    style_input_list.append(file)

您还可以进行其他一些简化,但这是最大的简化,它会为下一个简化打开大门。例如,既然您只做一件微不足道的事情而不是四行代码来实现有效的扩展,那么您可能可以摆脱continue

if os.path.isdir(image):
    for file in os.listdir(image):
        ext = os.path.splitext(file)[1]
        if ext.lower() in valid_ext:
            style_input_list.append(file)
else:
    style_input_list.append(file)

现在我们有了一个可以转化为列表推导式的片段——尽管我会使用生成器表达式:

if os.path.isdir(image):
    images = (file for file in os.listdir(image)
              if os.path.splitext(file)[1].lower() in valid_ext)
    style_input_list.extend(images)
else:
    style_input_list.append(image)

但是将整个事情变成一个列表理解将是非常丑陋的。您可以使用三元 if 将其转换为表达式,并在最后将整个内容展平,但如果您有五行代码,则不属于理解范围内。 (当然你可以把这五行分解成一个函数,然后将对该函数的调用包装成一个理解,这可能是值得的。)

【讨论】:

  • 感谢您的帮助!该函数实际上是基于我不久前编写的一些 Lua 代码,当时我不擅长编码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-10
  • 1970-01-01
  • 2015-06-05
  • 2021-08-19
  • 2012-10-05
  • 1970-01-01
  • 2018-03-24
相关资源
最近更新 更多