如何编写一个删除任何重复字符串的函数?在列表dates
中,字符串'2017-11-01 00:00:00','2018-12-01 00:00:00'
被重复了两次或两次以上。我想让它达到Expected Output
的位置,并且每个字符串只有1个大小写。
dates = ['2017-09-01 00:00:00', '2017-10-01 00:00:00', '2017-11-01 00:00:00', '2017-11-01 00:00:00', '2017-11-01 00:00:00', '2017-12-01 00:00:00', '2018-01-01 00:00:00', '2018-02-01 00:00:00', '2018-03-01 00:00:00', '2018-04-01 00:00:00', '2018-05-01 00:00:00', '2018-06-01 00:00:00', '2018-07-01 00:00:00', '2018-08-01 00:00:00', '2018-09-01 00:00:00', '2018-10-01 00:00:00', '2018-11-01 00:00:00', '2018-12-01 00:00:00', '2018-12-01 00:00:00', '2019-01-01 00:00:00']
预期输出:
['2017-09-01 00:00:00', '2017-10-01 00:00:00', '2017-11-01 00:00:00', '2017-12-01 00:00:00', '2018-01-01 00:00:00', '2018-02-01 00:00:00', '2018-03-01 00:00:00', '2018-04-01 00:00:00', '2018-05-01 00:00:00', '2018-06-01 00:00:00', '2018-07-01 00:00:00', '2018-08-01 00:00:00', '2018-09-01 00:00:00', '2018-10-01 00:00:00', '2018-11-01 00:00:00', '2018-12-01 00:00:00', '2019-01-01 00:00:00']
假设您想要过滤掉连续重复时,可以使用列表推导式:
[dates[0]]+[dates[i] for i in range(1, len(dates)) if dates[i-1]!=dates[i]]
或itertools.groupby
:
from itertools import groupby
[list(g)[0] for i,g in groupby(dates)]
输出:
['2017-09-01 00:00:00', '2017-10-01 00:00:00', '2017-11-01 00:00:00',
'2017-12-01 00:00:00', '2018-01-01 00:00:00', '2018-02-01 00:00:00',
'2018-03-01 00:00:00', '2018-04-01 00:00:00', '2018-05-01 00:00:00',
'2018-06-01 00:00:00', '2018-07-01 00:00:00', '2018-08-01 00:00:00',
'2018-09-01 00:00:00', '2018-10-01 00:00:00', '2018-11-01 00:00:00',
'2018-12-01 00:00:00', '2019-01-01 00:00:00']