我已向此发布了一个问题similar,但我仍然遇到过一些问题。
我有一个看起来像这样的元组列表:
[(1, 0.5, 'min'),
(2, 3, 'NA'),
(3, 6, 'NA'),
(4, 40, 'NA'),
(5, 90, 'NA'),
(6, 130.8, 'max'),
(7, 129, 'NA'),
(8, 111, 'NA'),
(9, 8, 'NA'),
(10, 9, 'NA'),
(11, 0.01, 'min'),
(12, 9, 'NA'),
(13, 40, 'NA'),
(14, 90, 'NA'),
(15, 130.1, 'max'),
(16, 112, 'NA'),
(17, 108, 'NA'),
(18, 90, 'NA'),
(19, 77, 'NA'),
(20, 68, 'NA'),
(21, 0.9, 'min'),
(22, 8, 'NA'),
(23, 40, 'NA'),
(24, 90, 'NA'),
(25, 92, 'NA'),
(26, 130.4, 'max')]
我正在进行实验,每个实验只有一个" min"和一个" max"值。我想总结第一个元素中的元素,最多只有一个" min"和"最大"例如,这个小数据集有3个实验,因为有3分钟和3个最大值。输出看起来像:
exp = [1+2+3+4+5+6+7+8+9+10, 11+12+13+14+15+16+17+18+19+20, 21+22+23+24+25+26]
我还想跟踪添加到列表中的值,以便我也有这个输出:
exp_values = [[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19, 20], [21, 22, 23, 24, 25, 26]]
我在尝试入门时遇到了麻烦,到目前为止只有一个大致的想法:
times = []
sum_
for item in tup_list:
if item[2] != "min":
sum_ += item[0]
times.append(sum_)
答案 0 :(得分:0)
Segmentation fault (core dumped)
你走了。我不认为python代码需要在这里描述。
vals = [(1, 0.5, 'min'),
(2, 3, 'NA'),
(3, 6, 'NA'),
(4, 40, 'NA'),
(5, 90, 'NA'),
(6, 130.8, 'max'),
(7, 129, 'NA'),
(8, 111, 'NA'),
(9, 8, 'NA'),
(10, 9, 'NA'),
(11, 0.01, 'min'),
(12, 9, 'NA'),
(13, 40, 'NA'),
(14, 90, 'NA'),
(15, 130.1, 'max'),
(16, 112, 'NA'),
(17, 108, 'NA'),
(18, 90, 'NA'),
(19, 77, 'NA'),
(20, 68, 'NA'),
(21, 0.9, 'min'),
(22, 8, 'NA'),
(23, 40, 'NA'),
(24, 90, 'NA'),
(25, 92, 'NA'),
(26, 130.4, 'max')]
it = iter(vals)
out = []
ap = [next(it)[0]]
for e,_,state in it:
if state == 'min':
out.append(ap)
ap = []
ap += [e]
out.append(ap)
print(out)
答案 1 :(得分:0)
这是一种处理它的简单方法:
l = []
for i in tup_list:
if 'min' in i:
try:
l.append(temp)
temp = []
except:
temp = []
temp.append(i[0])
if len(temp) > 0:
l.append(temp)
print l
[[1,2,3,4,5,6,7,8,9,10], [11,12,13,14,15,16,17,18,19, 20], [21, 22, 23, 24, 25, 26]]
这将获得分数列表,例如变量exp_values
。要获得这些值的总和,只需对列表求和:
sums = list(map(sum, l))
print sums
[55, 155, 141]
答案 2 :(得分:0)
这是一个解决方案,它跟踪作为每个实验一部分的指数,然后计算最后的总和。
tup_list = [(1, 0.5, 'min'),
(2, 3, 'NA'),
(3, 6, 'NA'),
(4, 40, 'NA'),
(5, 90, 'NA'),
(6, 130.8, 'max'),
(7, 129, 'NA'),
(8, 111, 'NA'),
(9, 8, 'NA'),
(10, 9, 'NA'),
(11, 0.01, 'min'),
(12, 9, 'NA'),
(13, 40, 'NA'),
(14, 90, 'NA'),
(15, 130.1, 'max'),
(16, 112, 'NA'),
(17, 108, 'NA'),
(18, 90, 'NA'),
(19, 77, 'NA'),
(20, 68, 'NA'),
(21, 0.9, 'min'),
(22, 8, 'NA'),
(23, 40, 'NA'),
(24, 90, 'NA'),
(25, 92, 'NA'),
(26, 130.4, 'max')]
all_experiment_data = []
current_experiment_data = []
for item in tup_list:
index, _, point_type = item
if point_type=="min" and current_experiment_data: #Starting a new experiment, flush the old one
all_experiment_data.append(current_experiment_data)
current_experiment_data = []
current_experiment_data.append(index)
#Flush the last experiment
all_experiment_data.append(current_experiment_data)
all_experiment_sums = [sum(experiment_indices) for experiment_indices in
all_experiment_data]
print("Indices in each of the experiments:")
print(all_experiment_data)
print("Sums of indices for experiments:")
print(all_experiment_sums)
这会产生:
Indices in each of the experiments:
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 10], [11, 12, 13, 14, 15, 16, 17, 18, 19, 20], [21, 22, 23, 24, 25, 26]]
Sums of indices for experiments:
[55, 155, 141]
答案 3 :(得分:0)
对于此序列,您需要跟踪min
和max
遇到的次数。一旦新的min
进入,它就是新序列的开始。所以我使用inSeq作为序列的跟踪器。让我知道它是否有帮助:
times = []
sum_ = 0
inSeq = 0
for item in tup_list:
if(item[2] == 'min' or item[2] == 'max'):
inSeq += 1
if(inSeq == 3): #start of new sequence when it hits new min
times.append(sum_)
sum_ = item[0]
inSeq = 1
else:
sum_ += item[0]