I have a set of file extensions (.jpg, .png, .exe, etc.) in a set called set_of_files
.
Below, I populate the keys of a dictionary from this set, while making the values None
.
The variable line[2]
contains the file size in bytes for each file extension.
My goal is to append line[2]
as the value to each file extension in the dictionary, and then add up the total bytes.
ie '.jpg': [1, 3, 4, 5]
-> '.jpg': [13]
However, I'm confused because I can't append to this dictionary (last line).
import re
# create set
set_of_files = set(list_of_files)
# populate dict
dicts = {key: None for key in list_of_files}
# go through and place total size by extension name
for line in open(file):
line = re.split('\s+', line)
for specific_line in set_of_files:
if line[3].endswith(specific_line):
dicts[specific_line].append(line[2])
You can't append it, because, you've initialised None
as values for dictionary.
import re
# create set
set_of_files = set(list_of_files)
# populate dict
_________________________ "None" is assigned here
|
\|/
dicts = {key: None for key in list_of_files}
# go through and place total size by extension name
for line in open(file):
line = re.split('\s+', line)
for specific_line in set_of_files:
if line[3].endswith(specific_line):
dicts[specific_line].append(line[2])
Instead, initialise an empty list []
, like this...
import re
# create set
set_of_files = set(list_of_files)
# populate dict
dicts = {key: [] for key in list_of_files}
# go through and place total size by extension name
for line in open(file):
line = re.split('\s+', line)
for specific_line in set_of_files:
if line[3].endswith(specific_line):
dicts[specific_line].append(line[2])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.