python中的切片列表

时间:2019-07-01 15:52:16

标签: python python-2.7

我在python中有一个这样的列表:

['37.26248', '23.16535', '2015-10-20 00:05:00', '2015-10-19 23:55:00', '40.36253', '19.16539', '2015-08-07 00:05:00', '2015-08-06 23:55:00', '37.26248', '23.16535', '2015-09-19 00:05:00', '2015-09-18 23:55:00', '37.26248', '23.16535', '2015-09-19 00:05:00', '2015-09-18 23:55:00', '35.87222', '14.5914', '2015-08-27 00:05:01', '2015-08-26 23:55:01']

我想每4个元素切片一个列表,并创建一个这样的列表列表:

[['37.26248', '23.16535', '2015-10-20 00:05:00', '2015-10-19 23:55:00'], ['40.36253', '19.16539', '2015-08-07 00:05:00', '2015-08-06 23:55:00'], ['37.26248', '23.16535', '2015-09-19 00:05:00', '2015-09-18 23:55:00'], ['37.26248', '23.16535', '2015-09-19 00:05:00', '2015-09-18 23:55:00'], ['35.87222', '14.5914', '2015-08-27 00:05:01', '2015-08-26 23:55:01']]

我尝试:

def list_slice(S, step):
    return [S[i::step] for i in range(step)]

list_after_slice.append(list_slice(original_list,(len(original_list)/4) ))

但是此函数混合输出并返回如下内容:

[[['38.83628', '25.76177', '2015-08-05 00:05:00', '2015-09-20 23:55:00'], ['18.87429', '2015-08-16 00:05:01', '2015-08-04 23:55:00', '37.70565'], ['2015-12-13 00:05:00', '2015-08-15 23:55:01', '37.70565', '27.01856'], ['2015-12-12 23:55:00', '36.55987', '27.01856', '2015-09-21 00:05:00'], ['34.24029', '17.7742', '2015-09-21 00:05:00', '2015-09-20 23:55:00']]]

有人知道如何解决此问题吗?

0 个答案:

没有答案