user12166681
user12166681

Reputation:

Why it only prints out one?

I have this bad boy:

def by_complexity(db : {str: {(int,int) : float}}) -> [(str,int)]:
    complex = []
    for state, taxes in db.items():
        complex.append((state, len(taxes.values())))
        return (sorted(complex, key = lambda zps : (-zps[1],zps[0])))



db1 = {'CT': {(      0,  12_499): .02,
                      ( 12_500,  49_999): .04, 
                      ( 50_000,    None): .06},

               'IN': {(0, None): .04},

               'LA': {(      0,   9_999): .03,
                      ( 10_000,  12_499): .05,
                      ( 12_500,  49_999): .055,
                      ( 50_000, 299_999): .06,
                      (300_000,    None): .078},

                'MA': {(0, None): .055}}
print(by_complexity(db1))

Now when I run it, it only prints out [('CT', 3)] instead of [('LA', 5), ('CT', 3), ('IN', 1), ('MA', 1)] so now I'm wondering why? because I can't find a bug in it... it just doesn't work

Upvotes: 0

Views: 47

Answers (1)

Arkenys
Arkenys

Reputation: 353

It's coming from your indent level with your return. You are returning while still in your for loop.

Try this :

    def by_complexity(db: {str: {(int, int): float}}) -> [(str, int)]:
        complex = []
        for state, taxes in db.items():
            complex.append((state, len(taxes.values())))
        return (sorted(complex, key=lambda zps: (-zps[1], zps[0])))

Upvotes: 1

Related Questions