1)如何从python列表元素中删除子字符串。我从这个论坛找到了一些主题,但没有得到针对我案例的正确提示。
2)如何删除公共日期,并且在下面的代码中,在具有相同数据的多个条目中只保留每个日期的一个条目。
['2010-01-29 07:30:02', '2010-01-29 07:55:04', '2014-02-05 08:51:39', '2014-05-23 01:29:06',
'2014-05-23 01:29:46', '2014-05-23 01:39:25', '2014-05-24 01:56:42', '2014-05-27 06:55:58',
'2014-05-27 07:10:48', '2014-05-31 02:04:58', '2014-06-10 07:41:12', '2014-06-11 09:56:48',
'2014-06-11 10:28:52',]
预期输出
['2010-01-2', '2014-02-05', '2014-05-23', '2014-05-24', '2014-05-27', '2014-05-31',
'2014-06-10', '2014-06-11']
答案 0 :(得分:3)
使用split()[0]
仅获取每个条目的日期,set
删除重复项,list
从集合中创建列表,最后sorted
对列表进行排序:
sorted(list(set(x.split()[0] for x in l)))
结果:
['2010-01-29',
'2014-02-05',
'2014-05-23',
'2014-05-24',
'2014-05-27',
'2014-05-31',
'2014-06-10',
'2014-06-11']
答案 1 :(得分:1)
您可以使用:sorted(list(set(i.split()[0] for i in l)))
示例运行:
>>> l=['2010-01-29 07:30:02', '2010-01-29 07:55:04', '2014-02-05 08:51:39', '2014-05-23 01:29:06',
... '2014-05-23 01:29:46', '2014-05-23 01:39:25', '2014-05-24 01:56:42', '2014-05-27 06:55:58',
... '2014-05-27 07:10:48', '2014-05-31 02:04:58', '2014-06-10 07:41:12', '2014-06-11 09:56:48',
... '2014-06-11 10:28:52']
>>> l
['2010-01-29 07:30:02', '2010-01-29 07:55:04', '2014-02-05 08:51:39', '2014-05-23 01:29:06', '2014-05-23 01:29:46', '2014-05-23 01:39:25', '2014-05-24 01:56:42', '2014-05-27 06:55:58', '2014-05-27 07:10:48', '2014-05-31 02:04:58', '2014-06-10 07:41:12', '2014-06-11 09:56:48', '2014-06-11 10:28:52']
>>> sorted(list(set(i.split()[0] for i in l)))
['2010-01-29', '2014-02-05', '2014-05-23', '2014-05-24', '2014-05-27', '2014-05-31', '2014-06-10', '2014-06-11']
>>>
答案 2 :(得分:0)
使用collections.Counter
>>> my_list = ['2010-01-29 07:30:02', '2010-01-29 07:55:04', '2014-02-05 08:51:39', '2014-05-23 01:29:06',
... '2014-05-23 01:29:46', '2014-05-23 01:39:25', '2014-05-24 01:56:42', '2014-05-27 06:55:58',
... '2014-05-27 07:10:48', '2014-05-31 02:04:58', '2014-06-10 07:41:12', '2014-06-11 09:56:48',
... '2014-06-11 10:28:52',]
>>> my_counter = collections.Counter(x.split()[0] for x in my_list)
>>> my_counter
Counter({'2014-05-23': 3, '2014-06-11': 2, '2014-05-27': 2, '2010-01-29': 2, '2014-05-31': 1, '2014-06-10': 1, '2014-05-24': 1, '2014-02-05': 1})
>>> my_counter.keys()
['2014-05-31', '2014-06-10', '2014-06-11', '2014-05-24', '2014-05-27', '2010-01-29', '2014-05-23', '2014-02-05']
使用itertools.groupby
:
>>> result = [x for x,y in itertools.groupby(sorted(x.split()[0] for x in my_list))]
>>> result
['2010-01-29', '2014-02-05', '2014-05-23', '2014-05-24', '2014-05-27', '2014-05-31', '2014-06-10', '2014-06-11']