根据条件进行列表拆分

时间:2019-10-02 16:05:38

标签: python list split

我有以下列表,需要根据其值的变化将其分为多个列表

1.0000000 46016.0000000 23.7303 8888.759 0.040 -0.5 -1.2 -1.99 -0.056 60 0 09:33:50 43076.39786 0.0000 2018/01/08  1.0000000 46016.0000000 22.9979 8888.758 0.065 -0.5 -1.7 -2.00 -0.057 60 0 09:35:00 43076.39867 0.0000 2018/01/08  1.0000000 46016.0000000 23.2420 8888.758 0.042 0.7 -2.0 -2.01 -0.057 60 0 09:36:05 43076.39942 0.0000 2018/01/08  1.0000000 46016.0000000 22.9979 8888.757 0.049 2.6 -2.1 -2.03 -0.057 60 0 09:37:10 43076.40017 0.0000 2018/01/08  1.0000000 46016.0000000 21.7772 8888.756 0.029 2.9 -1.9 -2.05 -0.058 60 0 09:38:16 43076.40093 0.0000 2018/01/08  1.0000000 46019.0000000 21.0447 8930.649 0.061 -0.4 -1.0 -2.10 -0.062 60 0 09:53:36 43076.41156 0.0000 2018/01/08  1.0000000 46019.0000000 21.0447 8930.648 0.056 0.0 -1.5 -2.11 -0.062 60 2 09:54:46 43076.41237 0.0000 2018/01/08  1.0000000 46019.0000000 21.0447 8930.650 0.044 1.3 -2.3 -2.11 -0.063 60 0 09:55:51 43076.41312 0.0000 2018/01/08  1.0000000 46019.0000000 20.8006 8930.649 0.046 0.4 -2.3 -2.13 -0.063 60 0 09:56:56 43076.41387 0.0000 2018/01/08  1.0000000 46019.0000000 20.0682 8930.649 0.042 0.6 -1.8 -2.14 -0.063 60 0 09:58:02 43076.41464 0.0000 2018/01/08  1.0000000 46015.0000000 20.8006 8916.477 0.043 -0.5 -1.3 -2.08 -0.069 60 0 10:24:02 43076.43266 0.0000 2018/01/08  1.0000000 46015.0000000 20.8006 8916.478 0.047 0.0 -2.1 -2.08 -0.070 60 0 10:25:12 43076.43347 0.0000 2018/01/08  1.0000000 46015.0000000 20.8006 8916.477 0.054 0.5 -2.4 -2.09 -0.070 60 0 10:26:17 43076.43422 0.0000 2018/01/08  1.0000000 46015.0000000 20.8006 8916.477 0.053 2.1 -2.8 -2.09 -0.070 60 0 10:27:22 43076.43497 0.0000 2018/01/08  1.0000000 46015.0000000 19.8240 8916.475 0.053 3.1 -2.9 -2.10 -0.070 60 0 10:28:28 43076.43574 0.0000 2018/01/08  1.0000000 46016.0000000 21.7772 8888.743 0.046 -0.3 0.0 -1.98 -0.075 60 0 10:59:40 43076.45737 0.0000 2018/01/08  1.0000000 46016.0000000 21.7772 8888.742 0.040 0.3 -0.8 -1.99 -0.075 60 0 11:00:50 43076.45818 0.0000 2018/01/08  1.0000000 46016.0000000 21.7772 8888.741 0.049 0.6 -1.3 -2.01 -0.076 60 0 11:01:55 43076.45893 0.0000 2018/01/08  1.0000000 46016.0000000 21.7772 8888.741 0.052 2.1 -1.6 -2.02 -0.076 60 0 11:03:00 43076.45968 0.0000 2018/01/08  1.0000000 46016.0000000 20.5565 8888.740 0.044 2.7 -1.7 -2.04 -0.076 60 0 11:04:06 43076.46044 0.0000 2018/01/08  1.0000000 46019.0000000 22.5096 8930.633 0.092 -1.8 1.0 -2.07 -0.077 60 0 11:20:05 43076.47152 0.0000 2018/01/08  1.0000000 46019.0000000 22.0213 8930.635 0.089 0.2 1.1 -2.08 -0.077 60 3 11:21:15 43076.47233 0.0000 2018/01/08  1.0000000 46019.0000000 22.2654 8930.632 0.100 -0.7 -0.4 -2.09 -0.077 60 0 11:22:20 43076.47308 0.0000 2018/01/08  1.0000000 46019.0000000 22.5096 8930.633 0.073 0.6 0.9 -2.11 -0.077 60 0 11:23:25 43076.47384 0.0000 2018/01/08  1.0000000 46019.0000000 21.7772 8930.630 0.074 2.3 0.2 -2.12 -0.078 60 0 11:24:31 43076.47460 0.0000 2018/01/08  1.0000000 46015.0000000 24.7068 8916.463 0.050 -2.2 -0.2 -2.10 -0.078 60 0 11:49:28 43076.49190 0.0000 2018/01/08  1.0000000 46015.0000000 24.7068 8916.464 0.052 -1.0 -1.3 -2.11 -0.078 60 0 11:50:38 43076.49271 0.0000 2018/01/08  1.0000000 46015.0000000 24.4627 8916.463 0.041 1.1 -2.3 -2.13 -0.078 60 0 11:51:43 43076.49346 0.0000 2018/01/08  1.0000000 46015.0000000 24.4627 8916.465 0.045 3.5 -3.0 -2.14 -0.078 60 0 11:52:48 43076.49421 0.0000 2018/01/08  1.0000000 46015.0000000 23.7303 8916.465 0.042 7.3 -2.9 -2.15 -0.078 60 0 11:53:54 43076.49497 0.0000 2018/01/08  1.0000000 46016.0000000 25.1951 8888.726 0.046 -3.1 -1.1 -2.03 -0.076 60 0 12:25:56 43076.51718 0.0000 2018/01/08  1.0000000 46016.0000000 24.9510 8888.725 0.066 -1.9 -1.8 -2.04 -0.076 60 0 12:27:06 43076.51799 0.0000 2018/01/08  1.0000000 46016.0000000 24.7068 8888.724 0.060 -1.8 -2.1 -2.04 -0.076 60 0 12:28:11 43076.51874 0.0000 2018/01/08  1.0000000 46016.0000000 24.4627 8888.722 0.036 -0.5 -2.4 -2.06 -0.076 60 0 12:29:16 43076.51949 0.0000 2018/01/08  1.0000000 46016.0000000 22.7537 8888.720 0.045 -0.2 -2.4 -2.0 -2.08 -0.076 60 0 12:30:22 43076.52025 0.0000 2018/01/08

对于此列表,我需要具有以下列表:

list1 =  [ 1.0000000  46016.0000000   23.7303   8888.759 0.040   -0.5   -1.2 -1.99 -0.056  60   0 09:33:50     43076.39786    0.0000  2018/01/08

,1.0000000 46016.0000000 22.9979 8888.758 0.065 -0.5 -1.7 -2.00 -0.057 60 0 09:35:00 43076.39867 0.0000 2018/01/08  ,1.0000000 46016.0000000 23.2420 8888.758 0.042 0.7 -2.0 -2.01 -0.057 60 0 09:36:05 43076.39942 0.0000 2018/01/08  ,1.0000000 46016.0000000 22.9979 8888.757 0.049 2.6 -2.1 -2.03 -0.057 60 0 09:37:10 43076.40017 0.0000 2018/01/08  ,1.0000000 46016.0000000 21.7772 8888.756 0.029 2.9 -1.9 -2.05 -0.058 60 0 09:38:16 43076.40093 0.0000 2018/01/08 ]

依次类推,直到最后一个

list7 = [ 1.0000000  46016.0000000   25.1951   8888.726 0.046   -3.1   -1.1 -2.03 -0.076  60   0 12:25:56     43076.51718    0.0000  2018/01/08

,1.0000000 46016.0000000 24.9510 8888.725 0.066 -1.9 -1.8 -2.04 -0.076 60 0 12:27:06 43076.51799 0.0000 2018/01/08  ,1.0000000 46016.0000000 24.7068 8888.724 0.060 -1.8 -2.1 -2.04 -0.076 60 0 12:28:11 43076.51874 0.0000 2018/01/08  ,1.0000000 46016.0000000 24.4627 8888.722 0.036 -0.5 -2.4 -2.06 -0.076 60 0 12:29:16 43076.51949 0.0000 2018/01/08  ,1.0000000 46016.0000000 22.7537 8888.720 0.045 -0.2 -2.4 -2.08 -0.076 60 0 12:30:22 43076.52025 0.0000 2018/01/08 ]

要检查是否已更改的值是tableau [11:27],例如“ 46016.0000000”,因此将创建一个新的子列表 感谢您的帮助 我尝试了这段代码,但省略了最后一个值:

    new_list = []

    if tableau[j][11:27].strip() == tableau[j+1][11:27].strip():
        new_list.append(tableau[j])
    else:
        new_list = []

4 个答案:

答案 0 :(得分:1)

您可以使用利用捕获组和反向引用的模式来匹配您的值。

(\d+\.\d+)(?: \1)*

regex demo | Python demo

然后您可以将匹配项分隔在一个空格上。

import re

regex = r"(\d+\.\d+)(?: \1)*"
test_str = "46016.0000000 46016.0000000 46016.0000000 46016.0000000 46016.0000000 46019.0000000 46019.0000000 46019.0000000 46019.0000000 46019.0000000 46015.0000000 46015.0000000 46015.0000000 46015.0000000 46015.0000000 46016.0000000 46016.0000000 46016.0000000 46016.0000000 46016.0000000 46019.0000000 46019.0000000 46019.0000000 46019.0000000 46019.0000000 46015.0000000 46015.0000000 46015.0000000 46015.0000000 46015.0000000 46016.0000000 46016.0000000 46016.0000000 46016.0000000 46016.0000000"
matches = re.finditer(regex, test_str, re.MULTILINE)
new_list = []

for matchNum, match in enumerate(matches, start=1):
    new_list.append(match.group().split(" "))

print(new_list)

输出

[['46016.0000000', '46016.0000000', '46016.0000000', '46016.0000000', '46016.0000000'], ['46019.0000000', '46019.0000000', '46019.0000000', '46019.0000000', '46019.0000000'], ['46015.0000000', '46015.0000000', '46015.0000000', '46015.0000000', '46015.0000000'], ['46016.0000000', '46016.0000000', '46016.0000000', '46016.0000000', '46016.0000000'], ['46019.0000000', '46019.0000000', '46019.0000000', '46019.0000000', '46019.0000000'], ['46015.0000000', '46015.0000000', '46015.0000000', '46015.0000000', '46015.0000000'], ['46016.0000000', '46016.0000000', '46016.0000000', '46016.0000000', '46016.0000000']]

答案 1 :(得分:0)

您可以循环浏览列表并在出现新值时创建列表

lists = []
lsi = []
for i1,i2 in zip(ls, ls[1:]):
    lsi.append(i1)
    if i1!=i2:
        lists.append(lsi)
        lsi = []
print(lists)

答案 2 :(得分:0)

如果您想变得时髦,可以使用列表理解。

from collections import defaultdict
dict_of_lists = defaultdict(list)
[dict_of_lists[i].append(i) for i in big_list]

最后是一个字典,其中的键是唯一值,而该dict的值就是您想要的列表。

答案 3 :(得分:0)

感谢您的帮助。我有一个对我有用的解决方法,它不是pythonic,但现在可以完成工作

首先,我建立一个发生更改的索引列表

if i1[11:27].strip()!=i2[11:27].strip():
    indexes.append(j-1)

然后,由于在普通文件中进行了6次更改,因此如果索引列表的长度等于6,则进行循环

chunks0 =chunks1 = chunks2 = chunks3 =chunks4 = chunks5= chunks6 = []

if len(indexes) == 6:
    print(indexes[0])
    chunks0 = tableau[0:indexes[0]+1]
    chunks1 = tableau[indexes[0] + 1: indexes[1]+1]
    chunks2 = tableau[indexes[1]+1: indexes[2]+1]
    chunks3 = tableau[indexes[2]+1: indexes[3]+1]
    chunks4 = tableau[indexes[3]+1: indexes[4]+1]
    chunks5 = tableau[indexes[4]+1: indexes[5]+1]
    chunks6 = tableau[indexes[5]+1: len(tableau)]

然后只需将每个块的最后五个元素写入输出文件中

for ele in chunks0[-5:]:
    f_out.write(ele + '\n')

for ele in chunks1[-5:]:
    f_out.write(ele + '\n')

for ele in chunks2[-5:]:
    f_out.write(ele + '\n')

for ele in chunks3[-5:]:
    f_out.write(ele + '\n')

for ele in chunks4[-5:]:
    f_out.write(ele + '\n')

for ele in chunks5[-5:]:
    f_out.write(ele + '\n')

for ele in chunks6[-5:]:
    f_out.write(ele + '\n')

就是这样。

欢迎任何帮助使代码更pythonic和更快的帮助!