简体   繁体   English

在Python中使用for循环从列表构建字典

[英]Building dictionary from list using for loop in Python

I have multiple format files in a directory. 我的目录中有多个格式文件。 I am trying to build a list or dictionary to group similar formatted (extension) files in python using for loop, but somehow it is not working. 我正在尝试建立一个列表或字典,以使用for循环将python中类似格式的文件(扩展名)分组,但是以某种方式无法正常工作。

Here is my sample code: 这是我的示例代码:

extension = ['pdf','xlsx','doc']

file_name_path=[]
file_dict ={}
for i in range(len(extension)):
    for file_name in filelst:
        if os.path.splitext(file_name)[-1] == extension[i]:
            file_name_path.append(file_name)
            file_dict[str(extension[i])]= file_name_path


file_name_path   
file_dict

where filelst is a list having all file names for example like 其中filelst是具有所有文件名的列表,例如

filelst = 
['PD_CFS_PLL_OnMonSummary_2017Q2.xlsx',
 'PD_Detailed_OMR_PLL_Lines_2017Q2.xlsx',
 'PD_Detailed_OMR_PLL_Loans_2017Q2.xlsx',
 'regexp-tip-sheet.pdf',
 'SAS statistical-business-analyst certification .pdf']

Alternative method to get dictionary with extensions as keys 获取扩展名为键的字典的替代方法

extension = ['.pdf','.xlsx','.doc']
filelist = ['one.pdf','two.pdf','three.doc','four.xlsx'] #just for example
d = dict()
for i in extension:
    d[i] = [j for j in filelist if os.path.splitext(j)[-1].lower()==i]
print(d)

output: 输出:

{'.doc': ['three.doc'], '.xlsx': ['four.xlsx'], '.pdf': ['one.pdf', 'two.pdf']}

Note that I used dots in extension list as os.path.splitext return list with last element being '.extension' . 请注意,我在扩展列表中使用点作为os.path.splitext返回列表,最后一个元素为'.extension' .lower() is used to made this solution case-insensitive, strings in extension list have to contain lowercase characters only. .lower()用于使此解决方案不区分大小写, extension列表中的字符串必须仅包含小写字符。

You are overwriting the dictionaries value each time the same key is seen. 每次看到相同的键都将覆盖字典值。

Instead, use a list as the value and append. 而是使用列表作为值并附加。 This is what defaultdict is for. 这就是defaultdict目的。

from collections import defaultdict 从集合导入defaultdict

extension = ['pdf','xlsx','doc']

file_dict = defaultdict(list)

for file_name in filelst:
    ext = os.path.splitext(file_name)[-1].lower()
    if ext in extension:
        file_dict[ext].append(file_name)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM