Python API: Get maximum number of elements in a component

Hello,

I need to get the component with the maximum number of elements. Right now I am doing it like this:

max_elements = 0 max_comp_name = str() for comp_name in mw.components(): num_elements = len(mw.component_elements(comp_name)) if num_elements > max_elements: max_elements = num_elements max_comp_name = comp_name

However, this is very slow. I'm thinking it's because mw.component_elements is returning a list. Are there any suggestions to get the number of elements in a component without generating a list?

Thanks,
Frank

Comments

  • It's probably slow because mw.component_elements() searches all elements to find ones belonging to the specified component.

    When you have a lot of components, it should be faster to iterate over all elements once and accumulate the number for each component using mw.element_component(element_id).
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!