简体   繁体   中英

List of Tuples to Dictionary with consolidated keys

Using Python 2.7, I have an arbitrarily long list of tuples (t) where:

  t[0] --> first_name_id
  t[1] --> first_name
  t[2] --> last_name_id
  t[3] --> last_name

first_name_id and first_name should be unique within the collection but last_name_id and last_name are unique only within the first_name_id/first_name.

As you can see below, there is a Jane whose last name is "Smith" but the id is 3 whereas for "Tom" the ID for "Smith" is "0"

t =  [('1', 'Tom', '0', 'Smith'),
    ('1', 'Tom', '1', 'Johnson'),
    ('1', 'Tom', '2', 'Williams'),
    ('32', 'Jane', '0', 'Brown'),
    ('32', 'Jane', '1', 'David'),
    ('32', 'Jane', '3', 'Smith'),
    .
    .
    .
    ]

I'm attepting to consolidate this into a dictionary so that I can quickly find a tuple of ID's by passing through the first_name and last_name:

So my data structure would be:

data_structure = {
    "Tom": {"first_name_id": "1", "surnames": {"Smith": "0", "Johnson": "1", "Williams": "3"}},
    "Jane": {"first_name_id": "32", "surnames": {"Brown": "0", "David": "1", "Smith": "3"}}
    }

output = data_structure["Tom"]["first_name_id"],data_structure["Tom"]["surnames"]["Williams"]

print output 

>>> ('1', '3')

Are you thinking something like this?

output = {}
for first_name_id, first_name, last_name_id, last_name in t:
    if not first_name in output:
        output[first_name] = {'first_name_id':first_name_id,'surnames':{}}
    output[first_name]['surnames'][last_name] = 

Since your data is already sorted on firstnames and firstname ids, you can apply itertools.groupby directly on your data to group on firstnames and firstname ids and then construct the inners dicts from the rest of the group data:

from collections import defaultdict 
from itertools import groupby

d = defaultdict(dict)

for (f_id, f), g in groupby(t, lambda x: (x[0], x[1])):
   d[f]['first_name_id'] = f_id
   d[f]['surnames'] = dict((k, v) for _, _, v, k in g)

defaultdict(<class 'dict'>,
            {'Jane': {'first_name_id': '32',
                      'surnames': {'Brown': '0', 
                                    'David': '1', 
                                    'Smith': '3'}},
             'Tom': {'first_name_id': '1',
                     'surnames': {'Johnson': '1',
                                  'Smith': '0',
                                  'Williams': '2'}}})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM