2014-10-22 3 views
-1

Я ищу самый быстрый способ повторно фактор, следующий список, который содержит dicts как элементыСортировка Python список contating dicts

[{u'domain': u'1d663096.bestapp243.biz', 
    u'flag_char_code': u'DR', 
    u'flag_hex': u'8081', 
    u'identifier': u'0000000002264A00', 
    u'indicator': u'Snd', 
    u'ip': u'172.30.133.105', 
    u'proto': u'UDP', 
    u'r_q': u'R Q', 
    u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .1d663096.bestapp243.biz.', 
    u'record': u'A', 
    u'status': u'NOERROR', 
    u'thread_id': u'1114', 
    u'timestamp': u'2014-10-12T11:20:27', 
    u'xid': u'aba2'}, 
{u'domain': u'1d663096.bestapp243.biz', 
    u'flag_char_code': u'DR', 
    u'flag_hex': u'8081', 
    u'identifier': u'0000000002264A00', 
    u'indicator': u'Snd', 
    u'ip': u'172.30.133.105', 
    u'proto': u'UDP', 
    u'r_q': u'R Q', 
    u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .1d663096.bestapp243.biz.', 
    u'record': u'A', 
    u'status': u'NOERROR', 
    u'thread_id': u'1114', 
    u'timestamp': u'2014-10-12T11:20:27', 
    u'xid': u'aba2'}, 
{u'domain': u'mgames.cf', 
    u'flag_char_code': u'DR', 
    u'flag_hex': u'8081', 
    u'identifier': u'000000000220ED40', 
    u'indicator': u'Snd', 
    u'ip': u'172.30.138.116', 
    u'proto': u'UDP', 
    u'r_q': u'R Q', 
    u'raw': u'10/13/2014 2:31:46 PM 110C PACKET 000000000220ED40 UDP Snd 172.30.138.116 f957 R Q [8081 DR NOERROR] A  .mgames.cf.', 
    u'record': u'A', 
    u'status': u'NOERROR', 
    u'thread_id': u'110C', 
    u'timestamp': u'2014-10-13T14:31:46', 
    u'xid': u'f957'}, 
    {u'domain': u'google.com', 
    u'flag_char_code': u'DR', 
    u'flag_hex': u'8081', 
    u'identifier': u'0000000002264A00', 
    u'indicator': u'Snd', 
    u'ip': u'172.30.133.105', 
    u'proto': u'UDP', 
    u'r_q': u'R Q', 
    u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .google.com.', 
    u'record': u'A', 
    u'status': u'NOERROR', 
    u'thread_id': u'1114', 
    u'timestamp': u'2014-10-12T11:20:27', 
    u'xid': u'aba2'}, 
{u'domain': u'qwe.domainsworkingsdromms.com', 
    u'flag_char_code': u'DR', 
    u'flag_hex': u'8381', 
    u'identifier': u'030E8D88', 
    u'indicator': u'Snd', 
    u'ip': u'172.27.29.77', 
    u'proto': u'UDP', 
    u'r_q': u'R Q', 
    u'raw': u'10/14/2014 10:37:13 AM 17E0 PACKET 030E8D88 UDP Snd 172.27.29.77 80eb R Q [8381 DR NXDOMAIN] A  .qwe.domainsworkingsdromms.com.', 
    u'record': u'A', 
    u'status': u'NXDOMAIN', 
    u'thread_id': u'17E0', 
    u'timestamp': u'2014-10-14T10:37:13', 
    u'xid': u'80eb'}] 

на выходе что-то вроде:

{ 
    '172.30.133.105': { 
    '1d663096.bestapp243.biz': 
     [ 
     {u'domain': u'1d663096.bestapp243.biz', 
      u'flag_char_code': u'DR', 
      u'flag_hex': u'8081', 
      u'identifier': u'0000000002264A00', 
      u'indicator': u'Snd', 
      u'ip': u'172.30.133.105', 
      u'proto': u'UDP', 
      u'r_q': u'R Q', 
      u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .1d663096.bestapp243.biz.', 
      u'record': u'A', 
      u'status': u'NOERROR', 
      u'thread_id': u'1114', 
      u'timestamp': u'2014-10-12T11:20:27', 
      u'xid': u'aba2'}, 
     {u'domain': u'1d663096.bestapp243.biz', 
      u'flag_char_code': u'DR', 
      u'flag_hex': u'8081', 
      u'identifier': u'0000000002264A00', 
      u'indicator': u'Snd', 
      u'ip': u'172.30.133.105', 
      u'proto': u'UDP', 
      u'r_q': u'R Q', 
      u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .1d663096.bestapp243.biz.', 
      u'record': u'A', 
      u'status': u'NOERROR', 
      u'thread_id': u'1114', 
      u'timestamp': u'2014-10-12T11:20:27', 
      u'xid': u'aba2'}, 
     ], 
    'google.com': 
     [ 
     {u'domain': u'google.com', 
     u'flag_char_code': u'DR', 
     u'flag_hex': u'8081', 
     u'identifier': u'0000000002264A00', 
     u'indicator': u'Snd', 
     u'ip': u'172.30.133.105', 
     u'proto': u'UDP', 
     u'r_q': u'R Q', 
     u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .google.com.', 
     u'record': u'A', 
     u'status': u'NOERROR', 
     u'thread_id': u'1114', 
     u'timestamp': u'2014-10-12T11:20:27', 
     u'xid': u'aba2'} 
     ] 
    }, 
    '172.30.138.116': { 
    'mgames.cf': 
     [ 
     {u'domain': u'mgames.cf', 
     u'flag_char_code': u'DR', 
     u'flag_hex': u'8081', 
     u'identifier': u'000000000220ED40', 
     u'indicator': u'Snd', 
     u'ip': u'172.30.138.116', 
     u'proto': u'UDP', 
     u'r_q': u'R Q', 
     u'raw': u'10/13/2014 2:31:46 PM 110C PACKET 000000000220ED40 UDP Snd 172.30.138.116 f957 R Q [8081 DR NOERROR] A  .mgames.cf.', 
     u'record': u'A', 
     u'status': u'NOERROR', 
     u'thread_id': u'110C', 
     u'timestamp': u'2014-10-13T14:31:46', 
     u'xid': u'f957'}   
     ] 
    } 
} 

Должен ли я разделить их в два списка, первый будет иметь ips в качестве ключей, а затем перебирать их, чтобы исправить домены в качестве ключей для sub dicts?

Любые идеи будут оценены. Благодаря

ответ

0

Если вам нужно повторно фактор, знать повторно учитываться структуру и сделать это явно

foo = {} 
for rec in data: 
    foo[rec['ip']] = {} 
    foo[rec['ip']][rec['domain']] = rec 

Примечание, если вы не ортодоксальным в использовании dict и достаточно открытым для defaultdict, autovivication может работать Wonder здесь,

>>> from collections import defaultdict 
>>> def tree(): return defaultdict(tree) 
>>> foo = tree() 
>>> for rec in data: 
    foo[rec['ip']][rec['domain']] = rec 

Пример

>>> import pprint 
>>> pprint.pprint(foo) 
{u'172.27.29.77': {u'qwe.domainsworkingsdromms.com': {u'domain': u'qwe.domainsworkingsdromms.com', 
                 u'flag_char_code': u'DR', 
                 u'flag_hex': u'8381', 
                 u'identifier': u'030E8D88', 
                 u'indicator': u'Snd', 
                 u'ip': u'172.27.29.77', 
                 u'proto': u'UDP', 
                 u'r_q': u'R Q', 
                 u'raw': u'10/14/2014 10:37:13 AM 17E0 PACKET 030E8D88 UDP Snd 172.27.29.77 80eb R Q [8381 DR NXDOMAIN] A  .qwe.domainsworkingsdromms.com.', 
                 u'record': u'A', 
                 u'status': u'NXDOMAIN', 
                 u'thread_id': u'17E0', 
                 u'timestamp': u'2014-10-14T10:37:13', 
                 u'xid': u'80eb'}}, 
u'172.30.133.105': {u'google.com': {u'domain': u'google.com', 
            u'flag_char_code': u'DR', 
            u'flag_hex': u'8081', 
            u'identifier': u'0000000002264A00', 
            u'indicator': u'Snd', 
            u'ip': u'172.30.133.105', 
            u'proto': u'UDP', 
            u'r_q': u'R Q', 
            u'raw': u'10/12/2014 11:20:27 AM 1114 PACKET 0000000002264A00 UDP Snd 172.30.133.105 aba2 R Q [8081 DR NOERROR] A  .google.com.', 
            u'record': u'A', 
            u'status': u'NOERROR', 
            u'thread_id': u'1114', 
            u'timestamp': u'2014-10-12T11:20:27', 
            u'xid': u'aba2'}}, 
u'172.30.138.116': {u'mgames.cf': {u'domain': u'mgames.cf', 
            u'flag_char_code': u'DR', 
            u'flag_hex': u'8081', 
            u'identifier': u'000000000220ED40', 
            u'indicator': u'Snd', 
            u'ip': u'172.30.138.116', 
            u'proto': u'UDP', 
            u'r_q': u'R Q', 
            u'raw': u'10/13/2014 2:31:46 PM 110C PACKET 000000000220ED40 UDP Snd 172.30.138.116 f957 R Q [8081 DR NOERROR] A  .mgames.cf.', 
            u'record': u'A', 
            u'status': u'NOERROR', 
            u'thread_id': u'110C', 
            u'timestamp': u'2014-10-13T14:31:46', 
            u'xid': u'f957'}}} 
0

Спасибо все, что я мог бы в состоянии сделать это:

Вот решение:

ips_as_keys = {} 
domains_as_sub_keys = {} 
for i in ES_result: 
    if i.get('ip') not in ips_as_keys: 
     if i.get('domain') not in domains_as_sub_keys: 
      domains_as_sub_keys[i.get('domain')] = [i] 
     else: 
      domains_as_sub_keys[i.get('domain')].append(i) 
     ips_as_keys[i.get('ip')] = [domains_as_sub_keys] 
    else: 
     ips_as_keys[i.get('ip')].append(domains_as_sub_keys) 
Смежные вопросы