简体   繁体   中英

Error while iterating list to put those into a key value pairs in python

I am new to python . I have a python list old_ecim_mims_list like below :-

['ReqSyncPort_v2_5_0', 'ECIM_SwM_v2_1_0_2_2', 'ECIM_SwM_v3_0_0_2_3', 'ResPowerDistribution_v1_0_0', 'ECIM_SwM_v4_2_0_3_2', 'ResPowerDistribution_v3_4_1', 'LratBb_v1_8025_0']

Now my requirement is here to iterate it and put it into a map like below key value pairs structure :-

ReqSyncPort=ReqSyncPort_v2_5_0
ECIM_SwM=ECIM_SwM_v2_1_0_2_2,ECIM_SwM_v3_0_0_2_3,ECIM_SwM_v4_2_0_3_2
ResPowerDistribution=ResPowerDistribution_v1_0_0,ResPowerDistribution_v3_4_1
LratBb=LratBb_v1_8025_0

I have done a sample program for this but I am getting error while executing :-

old_ecim_mims_map={} ;
for index , item in enumerate(old_ecim_mims_list) :
    print(index , item ) ;
    split_str=item.split("_v");
    #print(split_str[0]);
    if split_str[0] in old_ecim_mims_map :
        new_prop_map[split_str[0]].append(item);
        #old_ecim_mims_map.update({split_str[0]:item }) ;
    else :
        old_ecim_mims_map[split_str[0]]=item ;

Error :-

Traceback (most recent call last):
  File "F:/DeltaProject/com/dash/abinash/DeltaOperation/Createdelta.py", line 50, in <module>
    new_prop_map[split_str[0]].append(item);
AttributeError: 'str' object has no attribute 'append'

Suggest me where I am doing wrong .Searched lots of concepts , but those did not help me that much .Any help will be appreciated .

Your code fails because you add a string as value in dictionary (map), instead of enclosing it in [] to make an array in last line ( old_ecim_mims_map[split_str[0]]=item ). Next time you come across same key, you try to append to string, not to array.

What you have to do (and managed to do) is first check whether a certain key is already in a map. If it is, then you can append to the list old_ecim_mims_dict[key] . If there is no such key, a KeyError will be raised and then you have to create new list and put el inside it.

old_ecim_mims_list = ['ReqSyncPort_v2_5_0', 'ECIM_SwM_v2_1_0_2_2', 'ECIM_SwM_v3_0_0_2_3', 'ResPowerDistribution_v1_0_0', 'ECIM_SwM_v4_2_0_3_2', 'ResPowerDistribution_v3_4_1', 'LratBb_v1_8025_0']
old_ecim_mims_map = {}

for el in old_ecim_mims_list:
    key, _ = el.split('_v')
    try:
        old_ecim_mims_map[key].append(el)
    except KeyError:
        old_ecim_mims_map[key] = [el]

This code is much cleaner. If you want to rewrite your code, just change last line to

old_ecim_mims_map[split_str[0]]=[item]

Edit: As suggested in the comments, although I do not prefer this, it can be done by checking whether key is in map:

old_ecim_mims_list = ['ReqSyncPort_v2_5_0', 'ECIM_SwM_v2_1_0_2_2', 'ECIM_SwM_v3_0_0_2_3', 'ResPowerDistribution_v1_0_0', 'ECIM_SwM_v4_2_0_3_2', 'ResPowerDistribution_v3_4_1', 'LratBb_v1_8025_0']
old_ecim_mims_map = {}

for el in old_ecim_mims_list:
    key, _ = el.split('_v')
    if key in old_ecim_mims_map: # The same as if key in old_ecim_mims_map.keys()
        old_ecim_mims_map[key].append(el)
    else:
        old_ecim_mims_map[key] = [el]

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM