我想在不进行硬编码的情况下添加到我当前的字典中。我想通过添加-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']]发布于 2020-07-30 22:45:12
在我的回答中,有一些假设和更改,以便工作。如果不是硬编码,你的意思可能是键值可以改变,对吧?我将b_dict值更改为空列表,以便稍后添加所有商店、站点等。这是我的解决方案。真的希望这就是你要找的,因为你的问题很难理解。
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':[]}
a_dict = [(x[0], x[1] ,x[2]+"-"+k) for k, v in a_dict.items() for x in v]
for obj in a_dict:
for i, key in enumerate(b_dict.keys()):
b_dict[key] += [obj[i]]
print(a_dict)
# [('LA', 'Sallys', 'Associate-A'), ('Hollywood', 'Tonys', 'Shelf-A'), ('Compton', 'Sally', 'Shelves-A'), ('SAC', 'Sallys', 'Associate-B'), ('Townsland', 'Tonys', 'Shelf-B'), ('Compton', 'Tiffanys', 'Shelves-B')]
print(b_dict)
# {'Site': ['LA', 'Hollywood', 'Compton', 'SAC', 'Townsland', 'Compton'], 'Store': ['Sallys', 'Tonys', 'Sally', 'Sallys', 'Tonys', 'Tiffanys'], 'Station': ['Associate-A', 'Shelf-A', 'Shelves-A', 'Associate-B', 'Shelf-B', 'Shelves-B']}https://stackoverflow.com/questions/63175010
复制相似问题