如何简化 for 和 if 语句?

How do I simplify for and if statements?

如何简化这个?提前致谢:)

a_list = []

for a, blah in enumerate(bunch_of_As.blahs):
    if a.name == "WHATEVER":
        for msg in a:
            if msg.type == "value 1":
                a_list.append(a)
            elif msg.type == "value 2":
                if msg.time != 0:
                    do_something(a)
                a_list.remove(a)

可以简化这个,因为你在 a_list 上添加和删除 a 元素很多次都是不必要的

for a in bunch_of_As:
    if a.name == "WHATEVER":
        counter1 = 0
        counter2 = 0
        for msg in a:
            if msg.type == "value 1":
               counter1 += 1
            elif msg.type == "value 2":
               counter1 += -1
               if msg.time != 0:
                  counter2 += 1
       for n in range(counter1):
           a_list.append(a)

       for n in range(counter2):
           do_something(a)

只有我有两个问题或假设, 我假设 "value 1" =>(大于或等于)num "value 2" 的数量 do_something(a) 对 a 的影响为空

明明是多了行代码,却减少了不必要的append remove过程