问题描述
注意:我编辑了问题!
我在使用Python进行迭代时遇到麻烦,尤其是当我想将值总计到一定数量时.这里是我所面临的问题的更多信息:
I am having trouble with iteration in Python, especially when I would like to sum up values up to a certain number. Here's more information on the problem I'm facing:
我有一个看起来像这样的元组列表:
I have a list of tuples that looks like this:
[(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')]
我想对每个导致"max"的值和每个导致"min"的值求和,并将这些结果附加到两个单独的列表中.
I want to sum each value leading up to "max" and each value leading up to "min" and append these results to two separate lists.
例如,输出应为:
min_sums = [1+2+3+4+5,11+12+13+14, 21+22+23+24+15]
max_sums = [6+7+8+9+10, 15+16+17+18+19+20, 26]
我还想跟踪我实际求和的值,并将其作为输出:
I would also like to keep track of the values I am actually summing up and have this as an output as well:
min_sums_lst = [[1,2,3,4,5], [11,12,13,14],[21,22,23,24,15]]
max_sums_lst = [[6,7,8,9,10], [15,16,17,18,19,20], [26]]
我想我可以使用索引值,但是对于Python来说还很陌生,并且不确定如何继续.我正在学习生物学,但是我相信学习CS可以帮助我的工作.
I'm thinking I can use the index value, but am pretty new to Python and am not exactly sure how to proceed. I'm studying biology, but I believe learning CS could help with my work.
max_list = []
min_list = []
flag = ''
min_index = 0
max_index = float('inf');
if flag == 'h':
max_list.append(item)
elif flag == 'c':
min_list.append(item)
for i, item in enumerate(minmax_list):
print(i, item)
print("max_index: ", max_index)
print("min_index: ", min_index)
if item[2] == 'min':
min_index = i
max_list('h', item[0])
elif item[2] == 'NA' and (i < max_index):
max_list('h', item[0])
elif item[2] == 'max':
max_index = i
max_list('c', item[0])
elif item[2] == 'NA' and (i > min_index):
min_list('c', item[0])
我对Python还是很陌生-任何帮助将不胜感激.我只是试图根据上面的输出中所示的min和max在每个元组中添加第一项.
I'm quite new to Python - any help would be appreciated. I am only trying to add the first item in each tuple based on min and max as indicated in the output above.
推荐答案
我的回答与@Stefan的回答略有不同.它进行了更多的验证,您可以轻松添加"min"和"max"之外的其他类型.
My answer takes a slightly different approach to @Stefan's. It does a bit more validation, and you could pretty easily add other kinds besides 'min' and 'max'.
def partition_items(items):
lists = {
'min': [],
'max': [],
}
current_kind = None
current_list = None
for value, _, kind in items:
if kind != current_kind and kind != 'NA':
current_kind = kind
# You'll get a error here if current_kind isn't one of 'min'
# or 'max'.
current_list = lists[current_kind]
current_list.append(0)
# You'll get an error here if the first item in the list doesn't
# have type of 'min' or 'max'.
current_list[-1] += value
return lists
lists = partition_items(items)
print(lists['min'])
# -> [15, 50, 115]
print(lists['max'])
# -> [40, 105, 26]
这篇关于Python将元组列表中的值相加直到某些值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!