Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.00 KB | None | 0 0
  1. lines = sorted(list(open('coll_2019-03-18_16_43_01.txt','r',encoding='utf-8'))[5:])
  2. lines = [line.split() for line in lines] # разделяем на элементы
  3. lines = [line for line in lines if len(line) > 0] #фильтруем пустные
  4.  
  5. groups = [[lines[0]]] #инициализируем группы
  6.  
  7. def comp(a, b): # сравниваем элементы
  8.     return (len(a) == len(b)) and (a[:3] == b[:3])
  9.  
  10. def max_for_n(items, n): #получаем максимальный n-ый элемент из группы
  11.     values = [float(i[n]) for i in items]
  12.     return max(values)
  13.  
  14. for line in lines[1:]:
  15.     last_group = groups[-1]
  16.     last_word = last_group[0][0]
  17.     current_word = line[0]    
  18.     if comp(current_word, last_word):
  19.         last_group.append(line)
  20.     else:
  21.         groups.append([line])
  22.        
  23.  
  24. result = []
  25.  
  26. for group in groups:
  27.     first_line = group[0]
  28.  
  29.     for i in range(1, len(first_line)):
  30.         first_line[i] = max_for_n(group, i)
  31.  
  32.     result.append(first_line)
  33.  
  34. for g in result:
  35.     print(g)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement