I have a list unique string as below:
unique_region=['REGION-A', 'REGION-B','REGION-C','REGION-AB', 'REG-A','REG-B','REG-C', 'REG-AB','R-A','AB']
I would like to process the list and return a harmonized list with python as below:
harmonized_region=['REG-A', 'REG-B', 'REG-C', 'REG-AB', 'REG-A', 'REG-B','REG-C','REG-AB','REG-A','REG-AB']
I tried creating dictionary manually to match the unique data to the harmonized data, is there any better way to do this?
CodePudding user response:
I would do it following way
unique_region=['REGION-A', 'REGION-B','REGION-C','REGION-AB', 'REG-A','REG-B','REG-C', 'REG-AB','R-A','AB']
harmonized_region=['REG-' i.split('-')[-1] for i in unique_region]
print(harmonized_region)
gives output
['REG-A', 'REG-B', 'REG-C', 'REG-AB', 'REG-A', 'REG-B', 'REG-C', 'REG-AB', 'REG-A', 'REG-AB']
Explanation: I split at -
, get last element [-1]
and prefix it with REG-
. Observe that if -
is not present whole string become last element.
CodePudding user response:
I came to this conclusion:
unique_region=['REGION-A', 'REGION-B','REGION-C','REGION-AB', 'REG-A','REG-B','REG-C', 'REG-AB','R-A','AB']
harmonized_region=[]
for i in range(len(unique_region)):
harmonized_region.append('REG-' unique_region[i].split('-')[-1])
print(harmonized_region)
Result :
['REG-A', 'REG-B', 'REG-C', 'REG-AB', 'REG-A', 'REG-B', 'REG-C', 'REG-AB', 'REG-A', 'REG-AB']
In the comment above, you said that if there is no "-", you should leave it empty in the split() method because it separates based on space and puts it in the list.
I hope it is useful