简体   繁体   English

为什么只打印一个?

[英]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?现在当我运行它时,它只打印出[('CT', 3)]而不是[('LA', 5), ('CT', 3), ('IN', 1), ('MA', 1)]所以现在我想知道为什么? because I can't find a bug in it... it just doesn't work因为我找不到它的错误......它只是不起作用

It's coming from your indent level with your return.它来自您的缩进级别和您的返回。 You are returning while still in your for loop.您仍在 for 循环中返回。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM