Yes, I see, but have not yet found a beautiful solution. I do not like the idea of reading character by character file hands) with the validation like I know how to solve. While rummaging, that there is in python for such cases. The code that prinitspe of that php is approximately the same length will be
да, неправильно в таком случае мое решение
Yes, wrong in this case, my decision
import jsonimport timestart = time.time()with open('task-1.json') as data: f = json.load(data) print(len(f))with open('task-1.json') as data: content = data.read() row = json.loads(content) with open('res-task-1.json', 'w') as f: f.write('[') for item in row: if item.get('scores') >= 0.7: f.write(json.dumps(item)) f.write(',') f.write(']')print(time.time()-start)
две строки тут лишние - толкь для подсчета времени
в среднем выполняется за 0.03 сек на core i5 quad
было 10000 строк стало 2995
import json import time start = time.time () with open ( 'task-1.json') as data: f = json.load (data) print (len (f)) with open ( 'task-1.json') as data: content = data.read () row = json.loads (content) with open ( 'res-task-1.json', 'w') as f: f.write ( '[') for item in row: if item.get ( 'scores')> = 0.7: f.write (json.dumps (item)) f.write ( ',') f.write ( ']') print (time.time () - start)
two lines then the extra - Only for counting time
an average of 0.03 seconds is performed on the core i5 quad
It was 10,000 lines in 2995 was
Создавай тему, почему бы и нет
Create a theme, why not?
data = [ {'id': 0, 'media_category': 'clip'}, {'id': 1, 'media_category': 'promo'}, {'id': 2, 'media_category': 'promo'}, {'id': 3, 'meda_category': 'promo'}, {'id': 4, 'media_category': 'clip'}]def filter_data(data): return filter(lambda _data: _data.get('media_category') not in ['promo'], data) result = filter_data(data)for item in result: print(item)
собственно, сам код - строки 9-10, остальное - чтобы проверить в любой питоновской консоли. В рамках задачи мне потребовалось вставить 2 строки в существующий код - проверка на ключ, что нужно фильтровать и сама фильтрация.
Про скорость - понятно что го будет быстрее, но все это компилируется в сpython в реальности, там уже несильно важно.
Дв а принципе, я ничего не имею против кода на пхп, это не тема для срача)
Спасибо всем за участие, будете у нас в Минске - с удовольствием пересекусь за пивом. Думаю тема исчерпана
data = [ { 'Id': 0, 'media_category': 'clip'}, { 'Id': 1, 'media_category': 'promo'}, { 'Id': 2, 'media_category': 'promo'}, { 'Id': 3, 'meda_category': 'promo'}, { 'Id': 4, 'media_category': 'clip'}] def filter_data (data): return filter (lambda _data: _data.get ( 'media_category') not in [ 'promo'], data) result = filter_data (data) for item in result: print (item)
in fact, the code itself - lines 9-10, the rest - to check any pitonovskoj console. As part of the task, I need to insert 2 lines to the existing code - a key check, you need to filter and filtering itself.
About speed - it is clear that the first will be faster, but it is compiled into spython in reality, there has not much important.
Dve and principle, I have nothing against the code on php, this is not a topic for sracha)
Thank you all for your participation, we will have in Minsk - happy Crossing for a beer. I think the topic is exhausted
Проверь, если есть сомнения)