通过元素中的特定值将python列表拆分为列表

时间:2015-09-17 18:55:33

标签: python list split

我有一些特定值的列表(数字表示秒,小数点后是毫秒)。 当然这个列表要长得多,这里我只举一个简短的例子

TimeList = [173043.38, 173043.4, 173043.42, 173043.44, 173043.46, 173043.48, 173043.5, 173043.52, 173043.54, 173043.56, 173043.58, 173043.6, 173043.62, 173043.64, 173043.66, 173043.68, 173043.7, 173043.72, 173043.74, 173043.76, 173043.78, 173043.8, 173043.82, 173043.84, 173043.86, 173043.88, 173043.9, 173043.92, 173043.94, 173043.96, 173043.98, 173044.0, 173044.02, 173044.04, 173044.06, 173044.08, 173044.1, 173044.12, 173044.14, 173044.16, 173044.18, 173044.2, 173044.22, 173044.24, 173044.26, 173044.28, 173044.3, 173044.32, 173044.34, 173044.36, 173044.38, 173044.4, 173044.42, 173044.44, 173044.46, 173044.48, 173044.5, 173044.52, 173044.54, 173044.56, 173044.58, 173044.6, 173044.62, 173044.64, 173044.66, 173044.68, 173044.7, 173044.72, 173044.74, 173044.76, 173044.78, 173044.8, 173044.82, 173044.84, 173044.86, 173044.88, 173044.9, 173044.92, 173044.94, 173044.96, 173044.98, 173045.0, 173045.02, 173045.04, 173045.06, 173045.08, 173045.1, 173045.12, 173045.14, 173045.16, 173045.18, 173045.2, 173045.22, 173045.24, 173045.26, 173045.28, 173045.3, 173045.32, 173045.34, 173045.36, 173045.38, 173045.4, 173045.42, 173045.44, 173045.46, 173045.48, 173045.5, 173045.52, 173045.54, 173045.56, 173045.58, 173045.6, 173045.62, 173045.64, 173045.66, 173045.68, 173045.7, 173045.72, 173045.74, 173045.76, 173045.78, 173045.8, 173045.82, 173045.84, 173045.86, 173045.88, 173045.9, 173045.92, 173045.94, 173045.96, 173045.98, 173046.0, 173046.02, 173046.04, 173046.06, 173046.08, 173046.1, 173046.12, 173046.14, 173046.16, 173046.18, 173046.2, 173046.22, 173046.24, 173046.26, 173046.28, 173046.3, 173046.32, 173046.34, 173046.36, 173046.38, 173046.4, 173046.42, 173046.44, 173046.46, 173046.48, 173046.5, 173046.52, 173046.54, 173046.56, 173046.58, 173046.6, 173046.62, 173046.64, 173046.66, 173046.68, 173046.7, 173046.72, 173046.74, 173046.76, 173046.78, 173046.8, 173046.82, 173046.84, 173046.86, 173046.88, 173046.9, 173046.92, 173046.94, 173046.96, 173046.98, 173047.0, 173047.02, 173047.04, 173047.06, 173047.08, 173047.1, 173047.12, 173047.14, 173047.16, 173047.18, 173047.2, 173047.22, 173047.24]

我想将此列表拆分为列表,其中每个列表都包含第二个时间之间的值,如下所示:

TimeList1 = [173043.38, 173043.4, 173043.42, 173043.44, 173043.46, 173043.48, 173043.5, 173043.52, 173043.54, 173043.56, 173043.58, 173043.6, 173043.62, 173043.64, 173043.66, 173043.68, 173043.7, 173043.72, 173043.74, 173043.76, 173043.78, 173043.8, 173043.82, 173043.84, 173043.86, 173043.88, 173043.9, 173043.92, 173043.94, 173043.96, 173043.98]

TimeList2 = [173044.0, 173044.02, 173044.04, 173044.06, 173044.08, 173044.1, 173044.12, 173044.14, 173044.16, 173044.18, 173044.2, 173044.22, 173044.24, 173044.26, 173044.28, 173044.3, 173044.32, 173044.34, 173044.36, 173044.38, 173044.4, 173044.42, 173044.44, 173044.46, 173044.48, 173044.5, 173044.52, 173044.54, 173044.56, 173044.58, 173044.6, 173044.62, 173044.64, 173044.66, 173044.68, 173044.7, 173044.72, 173044.74, 173044.76, 173044.78, 173044.8, 173044.82, 173044.84, 173044.86, 173044.88, 173044.9, 173044.92, 173044.94, 173044.96, 173044.98]

TimeList3 = [173045.0, 173045.02, 173045.04, 173045.06, 173045.08, 173045.1, 173045.12, 173045.14, 173045.16, 173045.18, 173045.2, 173045.22, 173045.24, 173045.26, 173045.28, 173045.3, 173045.32, 173045.34, 173045.36, 173045.38, 173045.4, 173045.42, 173045.44, 173045.46, 173045.48, 173045.5, 173045.52, 173045.54, 173045.56, 173045.58, 173045.6, 173045.62, 173045.64, 173045.66, 173045.68, 173045.7, 173045.72, 173045.74, 173045.76, 173045.78, 173045.8, 173045.82, 173045.84, 173045.86, 173045.88, 173045.9, 173045.92, 173045.94, 173045.96, 173045.98]

TimeList4 = [173046.0, 173046.02, 173046.04, 173046.06, 173046.08, 173046.1, 173046.12, 173046.14, 173046.16, 173046.18, 173046.2, 173046.22, 173046.24, 173046.26, 173046.28, 173046.3, 173046.32, 173046.34, 173046.36, 173046.38, 173046.4, 173046.42, 173046.44, 173046.46, 173046.48, 173046.5, 173046.52, 173046.54, 173046.56, 173046.58, 173046.6, 173046.62, 173046.64, 173046.66, 173046.68, 173046.7, 173046.72, 173046.74, 173046.76, 173046.78, 173046.8, 173046.82, 173046.84, 173046.86, 173046.88, 173046.9, 173046.92, 173046.94, 173046.96, 173046.98]

TimeList5 =  [173047.0, 173047.02, 173047.04, 173047.06, 173047.08, 173047.1, 173047.12, 173047.14, 173047.16, 173047.18, 173047.2, 173047.22, 173047.24]

重要的假设是,第二个之间的元素数量并不总是相等。

任何想法如何解决这个问题。 我将非常感谢任何建议:)

1 个答案:

答案 0 :(得分:0)

您可以使用groupby from itertools

from itertools import groupby

TimeList = [173043.38, 173043.4, 173043.42, 173043.44, 173043.46, 173043.48, 173043.5, 173043.52, 173043.54, 173043.56, 173043.58, 173043.6, 173043.62, 173043.64, 173043.66, 173043.68, 173043.7, 173043.72, 173043.74, 173043.76, 173043.78, 173043.8, 173043.82, 173043.84, 173043.86, 173043.88, 173043.9, 173043.92, 173043.94, 173043.96, 173043.98, 173044.0, 173044.02, 173044.04, 173044.06, 173044.08, 173044.1, 173044.12, 173044.14, 173044.16, 173044.18, 173044.2, 173044.22, 173044.24, 173044.26, 173044.28, 173044.3, 173044.32, 173044.34, 173044.36, 173044.38, 173044.4, 173044.42, 173044.44, 173044.46, 173044.48, 173044.5, 173044.52, 173044.54, 173044.56, 173044.58, 173044.6, 173044.62, 173044.64, 173044.66, 173044.68, 173044.7, 173044.72, 173044.74, 173044.76, 173044.78, 173044.8, 173044.82, 173044.84, 173044.86, 173044.88, 173044.9, 173044.92, 173044.94, 173044.96, 173044.98, 173045.0, 173045.02, 173045.04, 173045.06, 173045.08, 173045.1, 173045.12, 173045.14, 173045.16, 173045.18, 173045.2, 173045.22, 173045.24, 173045.26, 173045.28, 173045.3, 173045.32, 173045.34, 173045.36, 173045.38, 173045.4, 173045.42, 173045.44, 173045.46, 173045.48, 173045.5, 173045.52, 173045.54, 173045.56, 173045.58, 173045.6, 173045.62, 173045.64, 173045.66, 173045.68, 173045.7, 173045.72, 173045.74, 173045.76, 173045.78, 173045.8, 173045.82, 173045.84, 173045.86, 173045.88, 173045.9, 173045.92, 173045.94, 173045.96, 173045.98, 173046.0, 173046.02, 173046.04, 173046.06, 173046.08, 173046.1, 173046.12, 173046.14, 173046.16, 173046.18, 173046.2, 173046.22, 173046.24, 173046.26, 173046.28, 173046.3, 173046.32, 173046.34, 173046.36, 173046.38, 173046.4, 173046.42, 173046.44, 173046.46, 173046.48, 173046.5, 173046.52, 173046.54, 173046.56, 173046.58, 173046.6, 173046.62, 173046.64, 173046.66, 173046.68, 173046.7, 173046.72, 173046.74, 173046.76, 173046.78, 173046.8, 173046.82, 173046.84, 173046.86, 173046.88, 173046.9, 173046.92, 173046.94, 173046.96, 173046.98, 173047.0, 173047.02, 173047.04, 173047.06, 173047.08, 173047.1, 173047.12, 173047.14, 173047.16, 173047.18, 173047.2, 173047.22, 173047.24]

for k, l in groupby(TimeList, key=lambda x: int(x)):
    print(str(k) + ":")
    print(list(l))

你得到:

173043:
[173043.38, 173043.4, 173043.42, 173043.44, 173043.46, 173043.48, 173043.5, 173043.52, 173043.54, 173043.56, 173043.58, 173043.6, 173043.62, 173043.64, 173043.66, 173043.68, 173043.7, 173043.72, 173043.74, 173043.76, 173043.78, 173043.8, 173043.82, 173043.84, 173043.86, 173043.88, 173043.9, 173043.92, 173043.94, 173043.96, 173043.98]
173044:
[173044.0, 173044.02, 173044.04, 173044.06, 173044.08, 173044.1, 173044.12, 173044.14, 173044.16, 173044.18, 173044.2, 173044.22, 173044.24, 173044.26, 173044.28, 173044.3, 173044.32, 173044.34, 173044.36, 173044.38, 173044.4, 173044.42, 173044.44, 173044.46, 173044.48, 173044.5, 173044.52, 173044.54, 173044.56, 173044.58, 173044.6, 173044.62, 173044.64, 173044.66, 173044.68, 173044.7, 173044.72, 173044.74, 173044.76, 173044.78, 173044.8, 173044.82, 173044.84, 173044.86, 173044.88, 173044.9, 173044.92, 173044.94, 173044.96, 173044.98]
173045:
[173045.0, 173045.02, 173045.04, 173045.06, 173045.08, 173045.1, 173045.12, 173045.14, 173045.16, 173045.18, 173045.2, 173045.22, 173045.24, 173045.26, 173045.28, 173045.3, 173045.32, 173045.34, 173045.36, 173045.38, 173045.4, 173045.42, 173045.44, 173045.46, 173045.48, 173045.5, 173045.52, 173045.54, 173045.56, 173045.58, 173045.6, 173045.62, 173045.64, 173045.66, 173045.68, 173045.7, 173045.72, 173045.74, 173045.76, 173045.78, 173045.8, 173045.82, 173045.84, 173045.86, 173045.88, 173045.9, 173045.92, 173045.94, 173045.96, 173045.98]
173046:
[173046.0, 173046.02, 173046.04, 173046.06, 173046.08, 173046.1, 173046.12, 173046.14, 173046.16, 173046.18, 173046.2, 173046.22, 173046.24, 173046.26, 173046.28, 173046.3, 173046.32, 173046.34, 173046.36, 173046.38, 173046.4, 173046.42, 173046.44, 173046.46, 173046.48, 173046.5, 173046.52, 173046.54, 173046.56, 173046.58, 173046.6, 173046.62, 173046.64, 173046.66, 173046.68, 173046.7, 173046.72, 173046.74, 173046.76, 173046.78, 173046.8, 173046.82, 173046.84, 173046.86, 173046.88, 173046.9, 173046.92, 173046.94, 173046.96, 173046.98]
173047:
[173047.0, 173047.02, 173047.04, 173047.06, 173047.08, 173047.1, 173047.12, 173047.14, 173047.16, 173047.18, 173047.2, 173047.22, 173047.24]