【发布时间】:2020-11-20 07:22:39
【问题描述】:
我想在不进行硬编码的情况下添加到我当前的字典中。我想通过添加 -A 并根据某人正在工作的站点来区分商店。
a_dict = {'A': [['LA', 'Sallys', 'Associate '], ['Hollywood', 'Tonys', 'Shelf'], ['Compton', 'Sally', 'Shelves']],'B': [['SAC', 'Sallys', 'Associate '], ['Townsland', 'Tonys', 'Shelf'], ['Compton', 'Tiffanys', 'Shelves']]}
b_dict = {'Site':"", 'Store':"", 'Station':""}
for key in a_dict:
b_dict.update(a_dict)
print(b_dict[key[0]])
这是代码当前打印出来的内容
[['LA', 'Sallys', 'Associate '], ['Hollywood', 'Tonys', 'Shelf'], ['Compton', 'Sally', 'Shelves']]
[['SAC', 'Sallys', 'Associate '], ['Townsland', 'Tonys', 'Shelf'], ['Compton', 'Tiffanys', 'Shelves']]
但我想让它打印出来
[['LA', 'Sallys', 'Associate '], ['Hollywood', 'Tonys', 'Shelf'], ['Compton', 'Sally', 'Shelves-A']]
[['SAC', 'Sallys', 'Associate '], ['Townsland', 'Tonys', 'Shelf'], ['Compton', 'Tiffanys', 'Shelves-A']]
【问题讨论】:
-
没有将
-A添加到任何字符串的代码。你为什么期望得到后一个结果? -
那么,我怎样才能在没有硬编码的情况下只将 A 添加到字符串中,而不是 -A?
-
如果不是硬编码,
A应该从哪里来? -
因此,如果 Station 值为 Shelves,我只希望它打印出 ShelvesA
-
您是否尝试过编写某种循环,包括像
if value == 'Shelves'这样的if语句,并使用像value + '-A'这样的字符串连接?
标签: python dictionary append key