[英]Python: Adding another value to an existing key dictionary
I am trying to take in all the files at a given path, and order them based on my data title names. 我正在尝试获取给定路径中的所有文件,并根据我的数据标题名称对其进行排序。 So my data title names are: 所以我的数据标题名称是:
data_titles = ['CPU','Physical_Disk','Memory','Network']
The files at this given path are named like 'CPU_data.txt'
and 'Memory_data.txt'
but there are also some that have more than one file per data title for example 'Physical_Disk_data_1.txt'
and 'Physical_Disk_data_2.txt'.
在此给定路径下的文件被命名为'CPU_data.txt'
和'Memory_data.txt'
但也有一些文件每个数据标题具有多个文件,例如'Physical_Disk_data_1.txt'
和'Physical_Disk_data_2.txt'.
I am trying to create a dicitonary in the style of: 我正在尝试以以下方式创建字典:
{'Network': 'Network_data.txt',
'Physical_Disk': ['Physical_Disk_data_1.txt','Physical_Disk_data_2.txt'],
'CPU': 'CPU_data.txt',
'Memory': 'Memory_data.txt'}
ie not overwriting older values 即不覆盖旧值
However I keep getting the error AttributeError: 'dict' object has no attribute 'update'
, if I use append instead of update I get a similar error AttributeError: 'dict' object has no attribute 'append'
但是,我不断收到错误AttributeError: 'dict' object has no attribute 'update'
,如果我使用append而不是update,我也会收到类似的错误AttributeError: 'dict' object has no attribute 'append'
table_csv_files={}
for file_names in os.listdir(Data_folder):
for name in data_titles:
if name in file_names:
if name in table_csv_files:
table_csv_files[name].update(file_names)
# Have also tried table_csv_files.append({name:file_names})
else:
table_csv_files[name]=file_names
print table_csv_files
What am I doing wrong? 我究竟做错了什么?
The problem is that your values are sometimes strings (eg for Network
) and sometimes lists (eg for Physical_Disk
). 问题在于您的值有时是字符串(例如,对于Network
),有时是列表(例如,对于Physical_Disk
)。 If you make sure that they are always lists you can easily append to them: 如果您确保它们始终是列表,则可以轻松地将它们附加到它们:
data_titles = ['CPU','Physical_Disk','Memory','Network']
table_csv_files={}
listdir_output = ['Network_data.txt', 'Physical_Disk_data_1.txt','Physical_Disk_data_2.txt', 'CPU_data.txt', 'Memory_data.txt']
for file_names in listdir_output:
for name in data_titles:
if name in file_names:
if name in table_csv_files:
table_csv_files[name].append(file_names)
else:
table_csv_files[name] = [file_names]
print table_csv_files
Output: 输出:
{'Memory': ['Memory_data.txt'], 'Physical_Disk': ['Physical_Disk_data_1.txt', 'Physical_Disk_data_2.txt'], 'Network': ['Network_data.txt'], 'CPU': ['CPU_data.txt']}
Use a defaultdict
whose default value is an empty list
, then you can append
to the dictionary values without worrying about which keys already exist in your dictionary. 使用默认值为默认list
为空list
的defaultdict
,然后可以append
到字典值中,而不必担心字典中已经存在哪些键。
from collections import defaultdict
table_csv_files = defaultdict(list)
for file_names in os.listdir(Data_folder):
for name in data_titles:
if name in file_names:
table_csv_files[name].append(file_names)
print table_csv_files
# {'CPU': ['CPU_data.txt'], 'Memory': ['Memory_data_1.txt', 'Memory_data_2.txt'], 'Physical_Disk': ['Physical_Disk_data.txt'], 'Network': ['Network_data.txt']}
According to Python documentation it's better to use defaultdict(), see the answer by Moses Koledoye 根据Python 文档 ,最好使用defaultdict(),请参见Moses Koledoye的答案
You cannot append to a dictionary. 您不能附加到字典。 You can append to a value corresponding to a key if that value is a list (or antoher object that implement the append method). 如果该值是一个列表 (或实现append方法的另一个对象),则可以将其追加到与键对应的值上。
I think this is the best way to make the code easy to follow. 我认为这是使代码易于遵循的最佳方法。 The function setdefault add a new list only if the key is not present. 仅当键不存在时,函数setdefault才会添加新列表 。
if name in file_names:
table_csv_files.setdefault(name, [])
table_csv_files[name].append(file_names)
here is a simple example of how you can add/update another value to an existing key in dictionary. 这是一个简单的示例,说明如何向字典中的现有键添加/更新另一个值。
my_dict = {'name': 'Rajiv Sharma', 'city': 'Delhi'}
print my_dict
my_dict['city'] = 'Kuala Lumpur'
print my_dict
output: 输出:
{'city': 'Delhi', 'name': 'Rajiv Sharma'}
{'city': 'Kuala Lumpur', 'name': 'Rajiv Sharma'}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.