I’m working in python and Revit, and I have a list of detail items with a name parameter. I’d like to filter my list down just the detail items where the name contains a partial match for any string in a list of partial matches. I have a working solution, but my intuition is telling me there ought to be a way to simplify it, since it doesn’t feel very readable to me.
This works:
JavaScript
x
6
1
filtered_detail_items = filter(lambda x: filter_partial_match(
2
key = x.LookupParameter('DMER_Panel_Name').AsString(),
3
partial_keywords = ['TAP BOX', 'VFD', 'CONTROL PANEL', 'DISC'],
4
inclusive = False),
5
detail_items)
6
JavaScript
1
15
15
1
def filter_partial_match(key, partial_keywords, inclusive = True):
2
3
# Allow user to pass in a single string or a list of strings.
4
# If a single string, treat it as a list.
5
if type(partial_keywords) is not list: partial_keywords = [ partial_keywords ]
6
7
match_found = False
8
if any(x in key for x in partial_keywords):
9
match_found = True
10
11
if inclusive:
12
return match_found
13
else:
14
return not match_found
15
This doesn’t:
JavaScript
1
2
1
filtered_detail_items = [(lambda x: (if any(y in x.LookParameter('DMER_Panel_Name').AsString() for y in ['TAP BOX', 'VFD', 'CONTROL PANEL', 'DISC']): x)) for x in detail_items ]
2
Advertisement
Answer
Per comments from Jeremy and Barmar, here is the final solution I used:
JavaScript
1
15
15
1
#filter out partial_matches that we don't want in the names.
2
partial_matches = ['TAP BOX', 'VFD', 'CONTROL PANEL', 'DISC']
3
4
first_item = riser_detail_items.FirstElement()
5
if first_item:
6
name_definition = first_item.LookupParameter('DMER_Panel_Name').Definition
7
8
riser_detail_items = [
9
x for x in riser_detail_items
10
if not any(
11
partial_match in x.get_Parameter(name_definition).AsString()
12
for partial_match in partial_matches
13
)
14
]
15