累计列表条件的总值并追加

时间:2018-04-05 08:54:36

标签: python count

我有一个嵌套列表,其中包含以下输出:

for i in my_list:
   print(i)

output:

['Event A', '15MAR18 121203']
['Event A', '15MAR18 134443']
['Event A', '15MAR18 151733']
['Event B', '15MAR18 165013']
['Event B', '15MAR18 182253']
['Event A', '15MAR18 195533']
['Event B', '15MAR18 212813']
['Event A', '15MAR18 221058']
['Event A', '15MAR18 230103']
['Event A', '15MAR18 234728']
['Event A', '16MAR18 003343']
['Event B', '16MAR18 012508']

我声明了一个变量image_size = 10

我希望我的输出如下:

['Event A', '15MAR18 121203', 'n/a']
['Event A', '15MAR18 134443', 'n/a']
['Event A', '15MAR18 151733', 'n/a']
['Event B', '15MAR18 165013', 30]
['Event B', '15MAR18 182253', 'n/a']
['Event A', '15MAR18 195533', 'n/a']
['Event B', '15MAR18 212813', 10]
['Event A', '15MAR18 221058', 'n/a']
['Event A', '15MAR18 230103', 'n/a']
['Event A', '15MAR18 234728', 'n/a']
['Event A', '16MAR18 003343', 'n/a']
['Event B', '16MAR18 012508', 40]

对于每个事件A:在最后一个事件B行上追加图像大小*事件A的数量。

E.g。第一个Event B的值为30 {3} Event A。以下Event B'n/a',因为当时与最后一个Event A之间没有Event B等等...每个{{1}之后计数会回到0 }。

我不确定该怎么做。

Event B

这至少会显示索引在哪些点上跳过事件B ...然后我可以用它以某种方式累积总数。但是对这样做没有信心。

3 个答案:

答案 0 :(得分:2)

使用生成器计算A个事件,当您遇到B事件并且该数字非零时,请添加该数字(乘以image_size)而不是{ {1}}占位符,并在每次执行时将计数重置为'n/a'

0

您可以使用以下内容生成新列表:

def add_event_count(events, image_size, to_count='Event A', placeholder='n/a'):
    event_count = 0
    for id_, info in events:
        third = placeholder
        if id_ == to_count:
            event_count += 1
        elif event_count:
            third = event_count * image_size
            event_count = 0
        yield [id_, info, third]

或只是循环:

new = list(add_event_count(my_list, image_size))

演示:

for i in add_event_count(my_list, image_size):
    print(i)

您也可以直接在>>> for i in add_event_count(my_list, image_size): ... print(i) ... ['Event A', '15MAR18 121203', 'n/a'] ['Event A', '15MAR18 134443', 'n/a'] ['Event A', '15MAR18 151733', 'n/a'] ['Event B', '15MAR18 165013', 30] ['Event B', '15MAR18 182253', 'n/a'] ['Event A', '15MAR18 195533', 'n/a'] ['Event B', '15MAR18 212813', 10] ['Event A', '15MAR18 221058', 'n/a'] ['Event A', '15MAR18 230103', 'n/a'] ['Event A', '15MAR18 234728', 'n/a'] ['Event A', '16MAR18 003343', 'n/a'] ['Event B', '16MAR18 012508', 40] 循环中执行相同操作,但将其打包在生成器函数中可让您重复使用该功能(例如,为for添加相同的计数计数Event A事件的条目)并使您的代码更具可读性。

答案 1 :(得分:2)

试试这个:

c=0
image_size=10
for i in mylist:
    if i[0] == 'Event A':
       c += 1
       i.append('n/a')
    else:
        if c==0:
           i.append('n/a')
        else:
           i.append(c*10)
print(mylist)

o / p将如下:

[['Event A', '15MAR18 121203', 'n/a'], 
 ['Event A', '15MAR18 134443', 'n/a'],
 ['Event A', '15MAR18 151733', 'n/a'], 
 ['Event B', '15MAR18 165013', 30], 
 ['Event B', '15MAR18 182253', 'n/a'], 
 ['Event A', '15MAR18 195533', 'n/a'],
 ['Event B', '15MAR18 212813', 10], 
 ['Event A', '15MAR18 221058', 'n/a'], 
 ['Event A', '15MAR18 230103', 'n/a'],
 ['Event A', '15MAR18 234728', 'n/a'],
 ['Event A', '16MAR18 003343', 'n/a'],
 ['Event B', '16MAR18 012508', 40]]

答案 2 :(得分:0)

您可以使用itertools.groupby

import itertools
s = [['Event A', '15MAR18 121203'], ['Event A', '15MAR18 134443'],['Event A', '15MAR18 151733'], ['Event B', '15MAR18 165013'], ['Event B', '15MAR18 182253'], ['Event A', '15MAR18 195533'], ['Event B', '15MAR18 212813'], ['Event A', '15MAR18 221058'], ['Event A', '15MAR18 230103'], ['Event A', '15MAR18 234728'], ['Event A', '16MAR18 003343'], ['Event B', '16MAR18 012508']]
new_result = [[a, list(b)] for a, b in itertools.groupby(s, key=lambda x:x[0])]
second_result = [b for _, b in new_result]
final_result = list(itertools.chain(*[[b+['n/a'] for b in c] if a == 'Event A' else [b+[10*len(second_result[d-1])] if not i else b+['n/a'] for i, b in enumerate(c)]  for d, [a, c] in enumerate(new_result)]))

输出:

[['Event A', '15MAR18 121203', 'n/a'],
 ['Event A', '15MAR18 134443', 'n/a'],
 ['Event A', '15MAR18 151733', 'n/a'],
 ['Event B', '15MAR18 165013', 30],
 ['Event B', '15MAR18 182253', 'n/a'],
 ['Event A', '15MAR18 195533', 'n/a'],
 ['Event B', '15MAR18 212813', 10],
 ['Event A', '15MAR18 221058', 'n/a'],
 ['Event A', '15MAR18 230103', 'n/a'],
 ['Event A', '15MAR18 234728', 'n/a'],
 ['Event A', '16MAR18 003343', 'n/a'],
 ['Event B', '16MAR18 012508', 40]]