为什么只打印一个?

时间:2019-10-17 08:37:42

标签: python python-3.x list dictionary tuples

我有这个坏男孩:

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))

现在,当我运行它时,它只会打印出[('CT', 3)] 而不是[('LA', 5), ('CT', 3), ('IN', 1), ('MA', 1)],所以现在我想知道为什么吗?因为我找不到其中的错误...它根本不起作用

1 个答案:

答案 0 :(得分:1)

它来自缩进级别和返回值。 您仍在for循环中返回。

尝试一下:

    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])))