我有一个大的CSV文件,我试图将其制作成较小的子列表。
唯一的问题是它没有做它应该做的事情......或者至少正确地分离数据只有一些东西被正确分开
我的问题:我有一个CSV文件,其数据深度在90到3米之间,而且这些数据会像这样来回传递。我正在使用最新的python。
每次数字再次增加时,我想将其分开。
离。 (深度)88,77,50,20,5,90,76,54,34,15,8,4,81,74,62,51,49,30,22,10,8 ......依此类推。它保持在90到3之间,依此类推。
我想要做的是每次在90到3之间分离数据。一旦它被分开,我想获取该列表中的最后和第一个值。像这样前。 88,77,50,20,5(此处分开),90,76,54,34,15,8,4(此处分开)81,74,62,51,49,30,22,10,8分开在这里)......等等。
这是我的代码:
#two sets of data that you test with.
m = ['9.90203', '9.79947', '9.66876', '9.54503', '9.42167', '9.2977', '9.17321', '9.05162', '8.11487', '8.038', '7.96111', '7.85746', '7.72701', '7.59709', '7.46781', '7.34127', '7.21842', '7.09548', '6.96798', '6.57355', '6.444', '6.32245', '6.23818', '6.15369', '6.04973', '4.63443', '4.49823', '4.36232', '4.21442', '4.0642', '26.5645', '26.4945', '26.4232', '26.3592', '26.2948', '23.418', '23.3667', '23.2756', '23.1856', '22.1849', '22.0822', '22.0191', '21.9671', '21.9154', '21.8641', '20.436', '20.3865', '20.3364', '20.2083', '20.0915', '19.9758', '19.8601', '19.7446', '19.6317', '18.1383', '17.9834', '17.8271', '17.7529', '17.6949', '17.6317', '17.5675', '17.4362', '16.581', '16.4745', '16.378', '16.2562', '16.1471', '16.0222', '15.89', '14.9752', '14.8725', '14.7666', '14.6234', '14.469', '14.3148', '13.7696', '13.6465', '13.5363', '13.4712', '13.4069', '13.3421', '13.2732', '13.1375', '13.0014', '12.4809', '12.3585', '12.2339', '12.121', '12.0116', '11.901', '11.7906', '11.6645', '11.5278', '11.3931', '11.2551', '11.1211', '10.993', '10.8501', '10.7205', '10.6026', '10.4992', '10.3952']
l = ['8.18142', '8.11846', '8.05988', '59.4627', '59.3455', '59.2296', '58.4541', '58.3302', '58.2265', '58.1239', '58.0314', '57.9405', '57.8496', '57.7514', '57.6746', '57.6098', '57.5414', '57.4722', '57.3517', '57.2151', '57.0934', '56.9704', '56.8361', '56.6993', '56.564', '56.4287', '56.3106', '56.2', '56.0877', '55.9782', '55.8869', '55.8223', '55.7578', '55.6933', '55.605', '55.4949', '55.3858', '55.2761', '55.1612', '55.0097', '54.8786', '54.7385', '54.6315', '54.5282', '54.4292', '54.327', '54.2217', '54.1127', '54.0037', '53.8938', '53.7695', '53.6271', '53.4857', '53.3437', '53.2254', '53.1712', '53.12', '53.0679', '53.0102', '52.9126', '52.8148', '52.7182', '52.6207', '52.491', '52.3561', '52.2267', '52.0908', '51.9405', '51.7786', '51.6421', '51.5043', '51.4063', '51.3087', '51.2117', '51.1141', '51.0304', '50.9446', '50.8577', '50.7744', '50.6716', '50.5655', '50.4625', '50.3598', '50.2569', '50.153', '50.0494', '49.9461', '49.8308', '49.7058', '49.582', '49.4583', '49.3473', '49.251', '49.1533', '49.0561', '48.953', '48.83', '48.7064', '48.5812', '48.4635', '48.3852', '48.2998', '48.2219', '48.1428', '48.0327', '47.9221', '47.8114', '47.7007', '47.5964', '47.4905', '47.3875', '47.2847', '47.1595', '47.0424', '46.9264']
group =0
temp = []
splited_list = {}
lengh = len(l)
for i in range(lengh):
if not i == lengh-1:
if l[i] > l[i+1]:
temp.append(l[i])
else:
temp.append(l[i])
group +=1
splited_list.update({str(group):temp})
temp = []
else:
if l[i] < l[-2]:
temp.append(l[i])
group +=1
splited_list.update({str(group):temp})
break
else:
group +=1
splited_list.update({str(group):[l[i]]})
break
print (splited_list)
这是我的输出:
{&#39; 1&#39;:[&#39; 8.18142&#39;,&#39; 8.11846&#39;,&#39; 8.05988&#39;,&#39; 59.4627& #39;,&#39; 59.3455&#39;,&#39; 59.2296&#39;,&#39; 58.4541&#39;,&#39; 58.3302&#39;,&#39; 58.2265& #39;,&#39; 58.1239&#39;,&#39; 58.0314&#39;,&#39; 57.9405&#39;,&#39; 57.8496&#39;,&#39; 57.7514& #39;,&#39; 57.6746&#39;,&#39; 57.6098&#39;,&#39; 57.5414&#39;,&#39; 57.4722&#39;,&#39; 57.3517& #39;,&#39; 57.2151&#39;,&#39; 57.0934&#39;,&#39; 56.9704&#39;,&#39; 56.8361&#39;,&#39; 56.6993& #39;,&#39; 56.564&#39;,&#39; 56.4287&#39;,&#39; 56.3106&#39;,&#39; 56.2&#39;,&#39; 56.0877& #39;,&#39; 55.9782&#39;,&#39; 55.8869&#39;,&#39; 55.8223&#39;,&#39; 55.7578&#39;,&#39; 55.6933& #39;,&#39; 55.605&#39;,&#39; 55.4949&#39;,&#39; 55.3858&#39;,&#39; 55.2761&#39;,&#39; 55.1612& #39;,&#39; 55.0097&#39;,&#39; 54.8786&#39;,&#39; 54.7385&#39;,&#39; 54.6315&#39;,&#39; 54.5282& #39;,&#39; 54.4292&#39;,&#39; 54.327&#39;,&#39; 54.2217&#39;,&#39; 54.1127&#39;,&#39; 54.0037& #39;,&#39; 53.8938&#39;,&#39; 53.7695&#39;,&#39; 53.6271&#39;, &#39; 53.4857&#39;,&#39; 53.3437&#39;,&#39; 53.2254&#39;,&#39; 53.1712&#39;,&#39; 53.12&#39;, &#39; 53.0679&#39;,&#39; 53.0102&#39;,&#39; 52.9126&#39;,&#39; 52.8148&#39;,&#39; 52.7182&#39;, &#;;;;;;;;;;;;;;; &#39; 51.9405&#39;,&#39; 51.7786&#39;,&#39; 51.6421&#39;,&#39; 51.5043&#39;,&#39; 51.4063&#39;, &#39; 51.3087&#39;,&#39; 51.2117&#39;,&#39; 51.1141&#39;,&#39; 51.0304&#39;,&#39; 50.9446&#39;, &#39; 50.8577&#39;,&#39; 50.7744&#39;,&#39; 50.6716&#39;,&#39; 50.5655&#39;,&#39; 50.4625&#39;, &#39; 50.3598&#39;,&#39; 50.2569&#39;,&#39; 50.153&#39;,&#39; 50.0494&#39;,&#39; 49.9461&#39;, &#39; 49.8308&#39;,&#39; 49.7058&#39;,&#39; 49.582&#39;,&#39; 49.4583&#39;,&#39; 49.3473&#39;, &#39; 49.251&#39;,&#39; 49.1533&#39;,&#39; 49.0561&#39;,&#39; 48.953&#39;,&#39; 48.83&#39;, &#;;;;;;;;;;;;;;;;;;;;; &#39; 48.2219&#39;,&#39; 48.1428&#39;,&#39; 48.0327&#39;,&# 39; 47.9221&#39;,&#39; 47.8114&#39;,&#39; 47.7007&#39;,&#39; 47.5964&#39;,&#39; 47.4905&#39;,&# 39; 47.3875&#39;,&#39; 47.2847&#39;,&#39; 47.1595&#39;,&#39; 47.0424&#39;,&#39; 46.9264&#39;]} < / p>
这是我想要的输出:
{&#39; 1&#39;:[&#39; 8.18142&#39;,&#39; 8.11846&#39;,&#39; 8.05988&#39;],&#39; 2& #39;:[&#39; 59.4627&#39;,&#39; 59.3455&#39;,&#39; 59.2296&#39;,&#39; 58.4541&#39;,&#39; 58.3302 &#39;,&#39; 58.2265&#39;,&#39; 58.1239&#39;,&#39; 58.0314&#39;,&#39; 57.9405&#39;,&#39; 57.8496 &#39;,&#39; 57.7514&#39;,&#39; 57.6746&#39;,&#39; 57.6098&#39;,&#39; 57.5414&#39;,&#39; 57.4722 &#39;,&#39; 57.3517&#39;,&#39; 57.2151&#39;,&#39; 57.0934&#39;,&#39; 56.9704&#39;,&#39; 56.8361 &#39;,&#39; 56.6993&#39;,&#39; 56.564&#39;,&#39; 56.4287&#39;,&#39; 56.3106&#39;,&#39; 56.2 &#39;,&#39; 56.0877&#39;,&#39; 55.9782&#39;,&#39; 55.8869&#39;,&#39; 55.8223&#39;,&#39; 55.7578 &#39;,&#39; 55.6933&#39;,&#39; 55.605&#39;,&#39; 55.4949&#39;,&#39; 55.3858&#39;,&#39; 55.2761 &#39;,&#39; 55.1612&#39;,&#39; 55.0097&#39;,&#39; 54.8786&#39;,&#39; 54.7385&#39;,&#39; 54.6315 &#39;,&#39; 54.5282&#39;,&#39; 54.4292&#39;,&#39; 54.327&#39;,&#39; 54.2217&#39;,&#39; 54.1127 &#39;,&#39; 54.0037&#39;,&#39; 53.8938&#39;,&#39; 53.7695&#39;,&#39 ; 53.6271&#39;,&#39; 53.4857&#39;,&#39; 53.3437&#39;,&#39; 53.2254&#39;,&#39; 53.1712&#39;,&#39 ; 53.12&#39;,&#39; 53.0679&#39;,&#39; 53.0102&#39;,&#39; 52.9126&#39;,&#39; 52.8148&#39;,&#39 ; 52.7182&#39;,&#39; 52.6207&#39;,&#39; 52.491&#39;,&#39; 52.3561&#39;,&#39; 52.2267&#39;,&#39 ; 52.0908&#39;,&#39; 51.9405&#39;,&#39; 51.7786&#39;,&#39; 51.6421&#39;,&#39; 51.5043&#39;,&#39 ; 51.4063&#39;,&#39; 51.3087&#39;,&#39; 51.2117&#39;,&#39; 51.1141&#39;,&#39; 51.0304&#39;,&#39 ; 50.9446&#39;,&#39; 50.8577&#39;,&#39; 50.7744&#39;,&#39; 50.6716&#39;,&#39; 50.5655&#39;,&#39 ; 50.4625&#39;,&#39; 50.3598&#39;,&#39; 50.2569&#39;,&#39; 50.153&#39;,&#39; 50.0494&#39;,&#39 ; 49.9461&#39;,&#39; 49.8308&#39;,&#39; 49.7058&#39;,&#39; 49.582&#39;,&#39; 49.4583&#39;,&#39 ; 49.3473&#39;,&#39; 49.251&#39;,&#39; 49.1533&#39;,&#39; 49.0561&#39;,&#39; 48.953&#39;,&#39 ; 48.83&#39;,&#39; 48.7064&#39;,&#39; 48.5812&#39;,&#39; 48.4635&#39;,&#39; 48.3852&#39;,&#39 ; 48.2998&#39;,&#39; 48.2219&#39;,&#39; 48.1428&#39;,&#39; 4 8.0327&#39;,&#39; 47.9221&#39;,&#39; 47.8114&#39;,&#39; 47.7007&#39;,&#39; 47.5964&#39;,&#39; 47.4905&#39;,&#39; 47.3875&#39;,&#39; 47.2847&#39;,&#39; 47.1595&#39;,&#39; 47.0424&#39;,&#39; 46.9264&#39;]}
答案 0 :(得分:0)
我认为你过度复杂了。请记住,要进行比较,首先要将字符串转换为浮点数。而且,没有必要通过其索引来引用每个点;您可以直接迭代list
对象。
另外,如上所述,使用由整数键入的字典有点不寻常,请考虑使用列表列表。
points = ['9.90203', '9.79947', '9.66876', '9.54503', '9.42167', '9.2977', '9.17321', '9.05162', '8.11487', '8.038',
'7.96111', '7.85746', '7.72701', '7.59709', '7.46781', '7.34127', '7.21842', '7.09548', '6.96798', '6.57355',
'6.444', '6.32245', '6.23818', '6.15369', '6.04973', '4.63443', '4.49823', '4.36232', '4.21442', '4.0642',
'26.5645', '26.4945', '26.4232', '26.3592', '26.2948', '23.418', '23.3667', '23.2756', '23.1856', '22.1849',
'22.0822', '22.0191', '21.9671', '21.9154', '21.8641', '20.436', '20.3865', '20.3364', '20.2083', '20.0915',
'19.9758', '19.8601', '19.7446', '19.6317', '18.1383', '17.9834', '17.8271', '17.7529', '17.6949', '17.6317',
'17.5675', '17.4362', '16.581', '16.4745', '16.378', '16.2562', '16.1471', '16.0222', '15.89', '14.9752',
'14.8725', '14.7666', '14.6234', '14.469', '14.3148', '13.7696', '13.6465', '13.5363', '13.4712', '13.4069',
'13.3421', '13.2732', '13.1375', '13.0014', '12.4809', '12.3585', '12.2339', '12.121', '12.0116', '11.901',
'11.7906', '11.6645', '11.5278', '11.3931', '11.2551', '11.1211', '10.993', '10.8501', '10.7205', '10.6026',
'10.4992', '10.3952']
data = dict()
idx = 1
last_point = float(points[0])
subset = [points[0]]
for point in points[1:]:
num_point = float(point)
if num_point > last_point:
data[idx] = subset
subset = [point]
idx += 1
else:
subset.append(point)
last_point = num_point
data[idx] = subset
答案 1 :(得分:0)
您可以尝试以下操作:
l = map(float, l)
results = [[ l[0] ]]
for i in l[1:]:
if i>results[-1][-1]:
results.append([i])
else:
results[-1].append(i)
这将为您提供列表清单。如果您真的想要字典,可以尝试:
resultsDict = {i:m for i, m in enumerate(results)}
或稍微缩短一下:
resultsDict = dict(enumerate(results))
我更喜欢上一个,因为它更具可读性......
如果您确实需要保留字符串,可以将比较i>results[-1][-1]
更改为float(i)>float(results[-1][-1])
,并删除最初的map
。