简体   繁体   中英

Replacing certain parts of a list in Python

I have a list of columns that represents months for different metrics. It was imported from a CSV. There are two issues: (1) The months start with '22-' and (2) some months end with .1 , .2 etc.

Here is the list:

months = ['22-Apr', '22-May', '22-Apr.1', '22-Apr.2']

Tried using .replace but I can't get it to remove only certain parts of the list

Among others, you could split by '-' and get the second part, then by '.' and get the first part. The latter will work even if there is no '.' in the string.

>>> months = ['22-Apr', '22-May', '22-Apr.1', '22-Apr.2']
>>> [s.split("-")[1].split(".")[0] for s in months]
['Apr', 'May', 'Apr', 'Apr']

The [1] means that this will work only if there is always a year-part present, but it does not matter if that has 2 or 4 digits.

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