如何在python中创建字典列表
我对 Python 比较陌生,实际上是对整个编程。不幸的是,我还没有能够在论坛上找到我的问题的答案。
我有一个具有不同文件扩展名的列表,文件扩展名出现多次。请参阅此处的示例:
extensions = ["JPG", "XLSX", "MP3", "PDF", "EXE", "PY", "XLSX", "DOCX", "JPG", "PPTX"]
我想使用上面的列表创建一个新的字典列表。它应该是这样的:
dicts = [{"Extension": "py", "Count": 1}, {"Extension": "docx", "Count": 1}]
我的计划是遍历列表并将文件扩展名作为新字典附加到新列表中,如上面的代码行所示。如果扩展名已作为字典列表中的字典存在,则仅["Count"]
匹配字典的索引应增加+=1
. 我已经编写了以下代码,但它不起作用。
我知道函数中的空扩展列表是一个问题,但我仍然没有让它按预期工作。我将不胜感激任何帮助。
extensions = ["JPG", "XLSX", "MP3", "PDF", "EXE", "PY", "XLSX", "DOCX", "JPG", "PPTX"]
def get_extensions(extensions):
extensionlist = []
for item in extensions:
extension = item.lower()
for dictionary in extensionlist:
if dictionary["Extension"] == extension:
dictionary["Count"] += 1
break
else:
extensionlist.append({"Extension": extension, "Count": 1})
break
return extensionlist
test = get_extensions(extensions)
print(test)
回答
您可以使用 a 构建频率表,Counter
然后对其进行迭代以构建您的列表:
from collections import Counter
extensions = ["JPG", "XLSX", "MP3", "PDF", "EXE", "PY", "XLSX", "DOCX", "JPG", "PPTX"]
frequencies = Counter(extensions)
# Build a list of dicts using a list comprehension. Not
# really sure why you'd want it in this format (rather
# than a dictionary).
output = [
{ "Extension": ext.lower(), "Count": freq }
for ext, freq in frequencies.items()
]
如果您想使用for
循环“手动”执行此操作,我建议采用类似的方法:首先构建一个包含频率计数的扩展键的字典,然后构建列表:
frequencies = {}
for extension in extensions:
# d.get(key, default) is like [], except it
# returns default if key is not in d (rather than
# throwing a KeyError).
frequencies[extension] = frequencies.get(extension, 0) + 1
# This is less idiomatic than the list comprehension
# shown above, but it's the same end result.
output = []
for extension, frequency in frequencies.items():
output.append(...)
这比你的双循环更好,for
因为它是一次传递extensions
,然后第二次传递frequencies
。即使您当前的实现有效,每次需要确定它是否已包含特定扩展名时,您都会对列表进行线性扫描(因此,在最坏的情况下,您需要检查1, 2, ..., n
输出列表中的元素您的m
扩展名)。