将列表添加到词典中?

时间:2020-08-24 04:52:07

标签: python list dictionary

我想制作一个字典,将其键作为目录中每个文件的扩展名。我想为每个键添加值,但是我想要的值数是两个。首先是具有每个扩展名的文件数,其次是具有该扩展名的每个文件的大小列表。

例如d={'py': 3,[23, 45, 67, 'PNG': 2,[345, 569]}

为此,我首先创建了一个完整列表,该列表的元素作为具有列表扩展名和每个文件大小的列表,分别为列表的第一个元素和第二个元素。我的问题是在每个扩展名中添加文件大小列表作为其值。我该如何更正我的代码?

count=0
list_full=[]

for root, dirs, files in os.walk(directory):
    for f in files:
        fullname = os.path.join(root, f)
        extension=fullname.split(".")[-1]
        size = os.path.getsize(fullname)
        print("The extension of " + fullname + "is : " + extension)
        
        list_full.append([extension,size])


list_first_half=[item[0] for item in list_full]

print("\nThe number of files with each extension is this:\n")
d={i:list_first_half.count(i) for i in list_first_half}
print(d)


for key in d:
    for item in list_full:
        if item[0]==key:
            d[key].append(item[1])




# if i[0]==key:
TypeError: 'int' object is not subscriptable

or for key in d:
    listforD=[]
    for i in range(len(list_full)):
        if list_full[i][0]==key:
            listforD.extend(list_full[i][1])
            d[key].append(listforD)

# listforD.extend(list_full[i][1])
TypeError: 'int' object is not iterable

2 个答案:

答案 0 :(得分:0)

我得到的是:

  1. list_full=[['txt', 68], ['pdf', 832053], ['pdf', 57741123], ['PNG', 101397], ['pdf', 4785477], ['txt', 0], ['zip', 649], ['zip', 665], ['py', 922], ['py', 1446], ['py', 730], ['py', 650], ['py', 818], ['docx', 62079], ['docx', 112673], ['docx', 15881], ['docx', 14388], ['docx', 14522], ['docx', 15279], ['docx', 17097], ['docx', 783905], ['txt', 6], ['lnk', 2119], ['lnk', 2090], ['txt', 7], ['py', 104], ['txt', 650], ['PNG', 31596], ['py', 893], ['txt', 5], ['pdf', 104857], ['pdf', 6214790], ['txt', 0], ['txt', 0], ['txt', 0], ['py', 247], ['py', 647]]

  2. d={'txt': 9, 'pdf': 5, 'PNG': 2, 'zip': 2, 'py': 9, 'docx': 8, 'lnk': 2}

我想打印出d={'txt':9,[68, 0, 650, ..., 0], ...}

答案 1 :(得分:0)

your answer开始,您在其中指定了自己的身份

list_full=[['txt', 68], ['pdf', 832053], ['pdf', 57741123], ['PNG', 101397], ['pdf', 4785477], ['txt', 0], ['zip', 649], ['zip', 665], ['py', 922], ['py', 1446], ['py', 730], ['py', 650], ['py', 818], ['docx', 62079], ['docx', 112673], ['docx', 15881], ['docx', 14388], ['docx', 14522], ['docx', 15279], ['docx', 17097], ['docx', 783905], ['txt', 6], ['lnk', 2119], ['lnk', 2090], ['txt', 7], ['py', 104], ['txt', 650], ['PNG', 31596], ['py', 893], ['txt', 5], ['pdf', 104857], ['pdf', 6214790], ['txt', 0], ['txt', 0], ['txt', 0], ['py', 247], ['py', 647]]

d={'txt': 9, 'pdf': 5, 'PNG': 2, 'zip': 2, 'py': 9, 'docx': 8, 'lnk': 2}

你可以做

for key, value in d.items(): # loop over d
    for e in list_full: # loop over list_full
        if key == e[0]: # check for matching doc types
            if type(d[key]) != list: # check if d[key] is already in desired format
                d[key] = [value, [e[1]]]
            else:
                d[key][1] += [e[1]]

请注意,这里您仅从list_full中获取d中的文档类型。

您将在d中获得所需的输出:

{'txt': [9, [68, 0, 6, 7, 650, 5, 0, 0, 0]],
 'pdf': [5, [832053, 57741123, 4785477, 104857, 6214790]],
 'PNG': [2, [101397, 31596]],
 'zip': [2, [649, 665]],
 'py': [9, [922, 1446, 730, 650, 818, 104, 893, 247, 647]],
 'docx': [8, [62079, 112673, 15881, 14388, 14522, 15279, 17097, 783905]],
 'lnk': [2, [2119, 2090]]}