【问题标题】:how to get python to return a list?如何让python返回一个列表?
【发布时间】:2013-01-31 03:46:32
【问题描述】:

所以我正在编写一个 python 脚本来清除无用和不需要的字符的文件名,但我遇到了一个问题,我似乎无法弄清楚如何返回包含所有字符的列表或字典我迭代过的项目。它只返回我迭代的第一个项目。这是我第一次用 python 写东西。任何帮助将不胜感激。我主要是为了学习而写这个。 clean_title() 方法是我想要返回的方法。我把它叫做底部。

import os
import re

# here is how all my video files will look after this
# show name Season 1 Episode 1

filename = os.listdir("E:/Videos/TV/tv-show")



def clean_title(filename):
    name = {}
    for title in filename:
        n_title = title.split('.')
        index = [i for i, item in enumerate(n_title) if re.search('\w\d{2}\w\d{2}', item)]
        if len(index) > 0:
            name = {'title':n_title[0:index[0]], 'ep_info':n_title[index[0]]}
            return name


def get_show_name(filename):
    pass


def update_title():
    #show_title = get_show_name + ' ' + get_episode_info
    #print show_title


if __name__=="__main__":
    test = clean_title(filename)
    print test

【问题讨论】:

  • 如果你想在列表中添加一些东西,使用 somelist.append(something)。在您的情况下,您已经创建了列表“season_ep_info”,因此调用 season_ep_info.append("something") 会将“something”添加到该列表中。

标签: python regex list return


【解决方案1】:

你有两个明显的问题。

首先是您从循环内部返回,因此当您点击return 语句时,您将只处理循环的一次迭代。这就是为什么看起来您获得了第一个迭代值,而实际上您从未达到其他迭代。

您可以通过将return 声明调整到正确的级别来解决此问题。

第二个问题是您从clean_title() 返回结果的方式。您只在 name 变量中存储了一个已清理的标题。每次通过循环,您都会用该迭代中的新标题覆盖先前计算的标题。即使您修复了 return 问题,当前版本也会返回您计算的 last 标题。

您可以在列表或字典中累积结果。如果是列表,则使用name = [] 进行初始化,并使用name.append(title_goes_here) 添加新标题。如果您想在字典中累积结果,请使用name = {} 进行初始化并使用name[index_goes_here] = title_goes_here 添加新标题。请注意,在字典的情况下,您需要有一些逻辑键值(通常是整数或字符串),以便稍后恢复该值。

最后,我必须补充一点,我发现您对复数对象和动作的使用单数(filenametitleclean_title)会造成混淆。我会调用文件名列表filenames,等等。

【讨论】:

  • 非常感谢,这非常有帮助。我将努力为事物制定更清晰的名称。我很感激这些提示。
【解决方案2】:

您在只查看了filename 中的第一个title 后返回

def clean_title(filename):
    # ...
    for title in filename:
        # ...
        if len(index) > 0:
            # ...
            return name

我建议让clean_title 只取一个文件名,并按如下方式遍历每个文件:

filenames = os.listdir("E:/Videos/TV/tv-show")
for filename in filenames:
    new_filename = clean_title(filename)
    # Rename file

【讨论】:

    【解决方案3】:

    循环将在返回时立即退出。我怀疑你想要做的是

    def clean_title(filename):
        names = []
        for title in filename:
            n_title = title.split('.')
            index = [i for i, item in enumerate(n_title) if re.search('\w\d{2}\w\d{2}', item)]
            if len(index) > 0:
                names.append({'title':n_title[0:index[0]], 'ep_info':n_title[index[0]]})
        return names    
    

    这将返回 dict 对象中的 list ,每个对象有两个键 titleep_info

    【讨论】:

      【解决方案4】:
      1. return name 的缩进错误,将其拉出到 同级name={}声明
      2. title 不应该是字符串,它应该是来自 for loop 的变量

      目前处于if条件下,因此只要条件为真,处理完条件返回中的语句就会执行

      def clean_title(filename):
          name_list = []
          for title in filename:
              n_title = title.split('.')
              index = [i for i, item in enumerate(n_title) if re.search('\w\d{2}\w\d{2}', item)]
              if len(index) > 0:
                  # you should store the dictionary in the lists, as all the dictionaries will contain the common keys of `ep_info`
                  name_list.append({title:n_title[0:index[0]], 'ep_info':n_title[index[0]]})
          return name_list # look here
      

      【讨论】:

        猜你喜欢
        • 2012-10-24
        • 2010-10-09
        • 1970-01-01
        • 1970-01-01
        • 2013-11-18
        • 1970-01-01
        • 2017-11-10
        • 2021-03-01
        • 1970-01-01
        相关资源
        最近更新 更多