I have a list of string labels. i want to keep the substring of very element before the second "." and remove all characters after the second ".". I found post that show how to do this with a text string using the split function. However, the list datatype does not have a split function. The actual data type is a pandas.core.indexes.base.index which appears to be a list to me. For the first element in the list, I want to keep L1.Energy
and remove everything after the second ".".
current_list = ['L1.Energy.Energy', 'L1.Utility.Energy', 'L1.Technology.Utility', 'L1.Financial.Utility']
desired_list = [L1.Energy', 'L1.Utility', 'L1.Technology,'L1.Financial']
Here as a oneliner:
desired_list = [ s[:s.find(".",s.find(".")+1)] for s in current_list]
current_list = ['L1.Energy.Energy', 'L1.Utility.Energy', 'L1.Technology.Utility', 'L1.Financial.Utility']
desired_list = [ '.'.join(x.split('.')[:2]) for x in current_list ]
BTW, this will work also if your labels have more than two dots (like 'L1.Utility.Energy.Electric'
)
Here, its ugly but it works
bob = ['L1.Energy.Energy', 'L1.Utility.Energy',
'L1.Technology.Utility','L1.Financial.Utility']
result = []
for i in bob:
temp = i.split(".")
result.append(temp[0] + "." + temp[1])
print(result)
A solution with regex
:
desired_list = [re.sub('(\..*)(\..*)',r'\1', s) for s in current_list]
Output:
['L1.Energy', 'L1.Utility', 'L1.Technology', 'L1.Financial']
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.