简体   繁体   中英

complicated list and dictionary lookup in python

I have a list of tuples and a dictionary of lists as follows.

# List of tuples
lot = [('Item 1', 43), ('Item 4', 82), ('Item 12', 33), ('Item 10', 21)]

# dict of lists
dol = {

    'item_category_one': ['Item 3', 'Item 4'],
    'item_category_two': ['Item 1'],
    'item_category_thr': ['Item 2', 'Item 21'],
}

Now I want to do a look-up where any item in any list within dol exists in any of the tuples given in lot . If this requirement is met, then i want to add another variable to that respective tuple.

Currently I am doing this as follows (which looks incredibly inefficient and ugly). I would want to know the most efficient and neat way of achieving this. what are the possibilities ?

PS: I am also looking to preserve the order of lot while doing this.

merged = [x[0] for x in lot]

for x in dol:
    for item in dol[x]:
        if item in merged:
            for x in lot:
                if x[0] == item:
                    lot[lot.index(x)] += (True, )

First, build a set of all your values inside of the dol structure:

from itertools import chain
dol_values = set(chain.from_iterable(dol.itervalues()))

Now membership testing is efficient, and you can use a list comprehension:

[tup + (True,) if tup[0] in dol_values else tup for tup in lot]

Demo:

>>> from itertools import chain
>>> dol_values = set(chain.from_iterable(dol.itervalues()))
>>> dol_values
set(['Item 3', 'Item 2', 'Item 1', 'Item 21', 'Item 4'])
>>> [tup + (True,) if tup[0] in dol_values else tup for tup in lot]
[('Item 1', 43, True), ('Item 4', 82, True), ('Item 12', 33), ('Item 10', 21)]

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