Good night, please tell me how to display only the latest values? I anticipate that the answer is on the surface, but I can not find it .. The fact is that this is only a hundredth part of the entire array and you need to cut off all unnecessary.

a = ['aa', '1', '2', '1.5', '3', '2.1', '0.52', 'ab', '3', '4'] def summ(list): list = float(list) item_list.append(list) length = len(item_list) s = sum(item_list) k = s/length mi = min(item_list) ma = max(item_list) return mi, ma, k for elements in a: if elements.startswith('a'): print('name', elements) item_list = [] else: print(summ(elements)) 

The following is displayed:

 name aa (1.0, 1.0, 1.0) (1.0, 2.0, 1.5) (1.0, 2.0, 1.5) (1.0, 3.0, 1.875) (1.0, 3.0, 1.92) (0.52, 3.0, 1.6866666666666665) name ab (3.0, 3.0, 3.0) (3.0, 4.0, 3.5) 

it is necessary that only the last entry be output, i.e.

 name aa (0.52, 3.0, 1.6866666666666665) name ab (3.0, 4.0, 3.5) 

    2 answers 2

    Just remember the latest indicators and print them when it is clear that there will be no further indicators:

     last = None for elements in a: if elements.startswith('a'): if last is not None: print(*last) last = None print('name', elements) item_list = [] else: last = summ(elements) if last is not None: print(*last) 
    • Thank you very much for the help! I did not think about this option - I thought it was in the code that there was an error in the output. - Kirill Shmelkov

    You can use numpy to speed up execution.

     import numpy as np a = ['aa', '1', '2', '1.5', '3', '2.1', '0.52', 'ab', '3', '4'] def summ(item_list): item_list = np.array(item_list).astype(np.float) mi = np.min(item_list) ma = np.max(item_list) k = np.divide(np.sum(item_list), item_list.size) return mi, ma, k dt = {} for elements in a: if elements.startswith('a'): ae = elements dt[ae] = [] else: dt[ae].append(elements) # {'aa': ['1', '2', '1.5', '3', '2.1', '0.52'], 'ab': ['3', '4']} for k in dt: print(k, summ(dt[k])) # aa (0.52, 3.0, 1.6866666666666665) # ab (3.0, 4.0, 3.5)