Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从原始目录返回文件名_Python_Python 3.x - Fatal编程技术网

Python 从原始目录返回文件名

Python 从原始目录返回文件名,python,python-3.x,Python,Python 3.x,我不太清楚这意味着什么 对于value_dict.items()中的(位置、日期、标题): builtins.AttributeError:“list”对象没有属性“items” 谁能帮我修一下这个吗 def create_date_dict(image_dict): '''(dict) -> dict Given an image dictionary, return a new dictionary where the key is a date and the value is

我不太清楚这意味着什么

对于value_dict.items()中的(位置、日期、标题): builtins.AttributeError:“list”对象没有属性“items” 谁能帮我修一下这个吗

def create_date_dict(image_dict):
'''(dict) -> dict

Given an image dictionary, return a new dictionary
where the key is a date and the value  is a list
of filenames of images taken on that date.

>>> d = {'image1.jpg': ['UTSC', '2017-11-03','Happy Friday']}
>>> date_d = create_date_dict(d)
>>> date_d == {'2017-11-03': ['image1.jpg']}
True
'''
new_d = {}
for (filenames, value_dict) in image_dict.items():
    for (location, date, caption) in value_dict.items():
        new_d[date] = {list(filename)}
return new_d

value\u dict
是一个列表。第二个
for
循环应该如下所示

for location, date, caption in value_dict:
利用易拆包的优势

编辑:

实际上,现在我再看一遍,你根本不需要另一个循环。照办

location, date, caption = value_dict
编辑2:

我不确定是什么原因导致了你的新错误。试试这个功能,它修复了我在你身上注意到的其他一些东西

from collections import defaultdict

def create_date_dict(image_dict):
    d = defaultdict(list)
    for image, (loc, date, cap) in image_dict.items():
         d[date].append(image)
    return d
它使用,这是标准库中一个方便的
dict
子类

编辑4:

如果没有
defaultdict
,这看起来像

def create_date_dict(image_dict):
    d = {}
    for image, (loc, date, cap) in image_dict.items():
         if date in d:
             d[date].append(image)
         else:
             d[date] = [image]
    return d

value\u dict
是一个列表。第二个
for
循环应该如下所示

for location, date, caption in value_dict:
利用易拆包的优势

编辑:

实际上,现在我再看一遍,你根本不需要另一个循环。照办

location, date, caption = value_dict
编辑2:

我不确定是什么原因导致了你的新错误。试试这个功能,它修复了我在你身上注意到的其他一些东西

from collections import defaultdict

def create_date_dict(image_dict):
    d = defaultdict(list)
    for image, (loc, date, cap) in image_dict.items():
         d[date].append(image)
    return d
它使用,这是标准库中一个方便的
dict
子类

编辑4:

如果没有
defaultdict
,这看起来像

def create_date_dict(image_dict):
    d = {}
    for image, (loc, date, cap) in image_dict.items():
         if date in d:
             d[date].append(image)
         else:
             d[date] = [image]
    return d

我需要位置、日期和标题周围的括号吗?好的,但在我测试它之后,它说由于某种原因lcoation没有定义。我如何定义它?通过使用键?@bigd为其设置索引,您就是在定义它。错误在哪一行?@bigd查看我的最新编辑。我还修复了我在代码中注意到的一些其他特性我是否需要位置、日期和标题周围的括号?好的,但在我测试它之后,它说由于某些原因没有定义lcoation。我如何定义它?通过使用键?@bigd为其设置索引,您就是在定义它。错误在哪一行?@bigd查看我的最新编辑。我修复了我在代码中注意到的一些其他特性,因为value_dict属于“list”类型。列表在默认情况下是可编辑的,不需要“.items()”请缩进代码,并显示如何实际调用函数以获取该错误。value\u dict的类型为“list”。列表在默认情况下是可编辑的,不需要“.items()”请缩进代码,并显示如何实际调用函数以获取该错误。