我正在尝试拆分一些数据,数据采用这种形式......
['20150406,34.4800,34.8100,34.2300,34.4200,21480500', '20150407,34.5400,34.8900,34.5100,34.6300,14331200']
列表中每个字符串中的第一项是日期,我正在尝试在选定日期拆分数据。但是有整个字符串...例如,如果我选择的日期是2015-04-07,那么上面的数据会像这样分开......
['20150406,34.4800,34.8100,34.2300,34.4200,21480500']
['20150407,34.5400,34.8900,34.5100,34.6300,14331200']
这也必须适用于包含大量字符串的列表,其形式与此相同......
答案 0 :(得分:3)
使用next()
和enumerate()
查找包含所需日期的字符串位置,然后 slice :
next()
情侣笔记:
StopIteration
将通过try/except
例外 - 您应该使用-1
处理它,或者提供默认值next((i for i, item in enumerate(l) if item.startswith(d)), -1)
例如:
date
检查日期是否与所需日期匹配,我们只是检查项目是否以特定日期字符串开头。如果所需日期为datetime
或strftime()
,则需要事先使用>>> from datetime import datetime
>>> d = datetime(2015, 4, 7)
>>> d = d.strftime("%Y%m%d")
>>> d
'20150407'
对其进行格式化:
rotate90 = (map reverse .) $ foldr (zipWith (:)) $ repeat ""
答案 1 :(得分:2)
我认为你想要一个groupby,将不以日期开头的字符串分组,并且这样做的日期会分隔这些组:
l = ['20150406,34.4800,34.8100,34.2300,34.4200,21480500', '2015010,34.5400,34.8900,34.5100,34.6300,14331200'
, '20150407,34.5400,34.8900,34.5100,34.6300,14331200']
dte = "2015-04-07"
delim = dte.replace("-","") + ","
from itertools import groupby
print([list(v) for k,v in groupby(l,key=lambda x: not x.startswith(delim))])
[['20150406,34.4800,34.8100,34.2300,34.4200,21480500', '2015010,34.5400,34.8900,34.5100,34.6300,14331200'], ['20150407,34.5400,34.8900,34.5100,34.6300,14331200']]
groupby将继续将数据拆分多次,因为字符串以日期开头。
答案 2 :(得分:1)
从alecxe回答延伸: 代码可以按输入日期将原始列表拆分为耦合子列表。
l = [
... '20150406,34.4800,34.8100,34.2300,34.4200,21480500',
... '20160402,34.1,32.8100,33.2300,31.01,22282510',
... '20150407,34.5400,34.8900,34.5100,34.6300,14331200',
... '20120101,2.540,14.8201,32.00,30.1311,12331230',
... '20150407,34.5400,34.8900,34.5100,34.6300,14331200',]
index = [i for i, item in enumerate(l) if item.startswith(d)]
[l[i:j] for i, j in zip([0]+index, index+[None])]
output:
[['20150406,34.4800,34.8100,34.2300,34.4200,21480500', '20160402,34.1,32.8100,33.2300,31.01,22282510'], ['20150407,34.5400,34.8900,34.5100,34.6300,14331200', '20120101,2.540,14.8201,32.00,30.1311,12331230'], ['20150407,34.5400,34.8900,34.5100,34.6300,14331200']]