Home > Software design >  Find the most compact and efficient version of this python function
Find the most compact and efficient version of this python function

Time:07-19

Here is a python function I have defined to assign a class (in the form of an integer) to every node of a networkx object. As you can see, the function is pretty long and most-likely not so efficient because of the for and nested if-else statements. I want to do the same job but instead more efficient pythonic way. Is there any way to compress the following python function into much smaller size?

def node_to_community_creator(labels: dict, communities: List) -> dict:
    """This function assigns a community to each node of a network depending on
    the communities list"""

    node_to_community = {}
    for label, id in labels.items():
        if len(communities[0]) > 1:
            if len(communities[0]) == 2:
                if label in communities[0][0]:
                    node_to_community[label] = 0
                else:
                    node_to_community[label] = 1
            elif len(communities[0]) == 3:
                if label in communities[0][0]:
                    node_to_community[label] = 0
                elif label in communities[0][1]:
                    node_to_community[label] = 1
                else:
                    node_to_community[label] = 2
            elif len(communities[0]) == 4:
                if label in communities[0][0]:
                    node_to_community[label] = 0
                elif label in communities[0][1]:
                    node_to_community[label] = 1
                elif label in communities[0][2]:
                    node_to_community[label] = 2
                else:
                    node_to_community[label] = 3
            elif len(communities[0]) == 5:
                if label in communities[0][0]:
                    node_to_community[label] = 0
                elif label in communities[0][1]:
                    node_to_community[label] = 1
                elif label in communities[0][2]:
                    node_to_community[label] = 2
                elif label in communities[0][3]:
                    node_to_community[label] = 3
                else:
                    node_to_community[label] = 4
            elif len(communities[0]) == 6:
                if label in communities[0][0]:
                    node_to_community[label] = 0
                elif label in communities[0][1]:
                    node_to_community[label] = 1
                elif label in communities[0][2]:
                    node_to_community[label] = 2
                elif label in communities[0][3]:
                    node_to_community[label] = 3
                elif label in communities[0][4]:
                    node_to_community[label] = 4
                else:
                    node_to_community[label] = 5
            elif len(communities[0]) == 7:
                if label in communities[0][0]:
                    node_to_community[label] = 0
                elif label in communities[0][1]:
                    node_to_community[label] = 1
                elif label in communities[0][2]:
                    node_to_community[label] = 2
                elif label in communities[0][3]:
                    node_to_community[label] = 3
                elif label in communities[0][4]:
                    node_to_community[label] = 4
                elif label in communities[0][5]:
                    node_to_community[label] = 5
                else:
                    node_to_community[label] = 6
        else:
            node_to_community[label] = 0
    return node_to_community

CodePudding user response:

Your code has many work to improve. First, you must modify your code like below:

if len(c...[0])>0:
    if len(c...[0])==1:
        ...
    elif len(c...[0])==2:
        ...

Must replace with

if len(c...[0])==1:
    ...
if len(c...[0])==2:
    ...

It is a principle in ZEN of python flat is better than hierarchi. Also you can

elif len(communities[0]) == 5:
    if label in communities[0][0]:
        node_to_community[label] = 0
    elif label in communities[0][1]:
        node_to_community[label] = 1
    elif label in communities[0][2]:
        node_to_community[label] = 2
    elif label in communities[0][3]:
        node_to_community[label] = 3
    else:
        node_to_community[label] = 4

replace with:

try:
    a = communities[0].index(label)
except:
    a = len(communities[0]) -1
finally:
    node_to_community[label] = a

after this you can also improve your code, for start apply this

  • Related