Skip to content
Advertisement

Searching for matching attributes

My special objects have one list of attribute-values (that is a varying list of abbreviated strings of short but differing length meaning some capabilities of any object) → ['A', 'B', 'AC', 'BC', ...]

running the program I’m searching for at least one existing match out of a varying search-list → ['BC', 'BP', 'X', ...]

At the moment I solved that by iterating over each search-list element using an in-operation against every attribute-list-like:

for e in all_elements:
    for a in valid_attributes:
        if a in e.capabilities:
            handle_match(e, a)

For only a handful of attributes and few elements, it will be endurable in need of time, but…

The further problem: get all a-values at once and handle them in same moment and after that continuing with next e-element.

I thought for a moment about to give each a-value a bit-position inside a big word of currently around 320 bit width and check any occurrence by an and-operation; other idea to use an enumeration for attributes; but my tries hadn’t been successful so far.

Does anyone have a better and easier idea? Currently my 1½ year knowledge of Python looks insufficient.


capabilties are strings of length 1,2 or 3 characters, all string-values are unique in elements as well in valid-attributes, ‘A’, ‘B’ vs. ‘AB’ or ‘BA’ has no common feature or meaning

Advertisement

Answer

If the attributes are not repeated you can store them as a set instead and calculate the intersection:

for e in all_elements:
    if len(valid_attributes.intersection(e.capabilities))>0:
        handle_match(e, a)

Or at a greater space sacrifice you can store the attribute list as a sparse binary list, where each element represents the existence of some attribute. Then you can check attributes in constant time.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement