你会如何抽象这一系列的列表继续条件?

时间:2016-11-13 13:12:43

标签: python iteration

我有一些源代码有许多条件会导致跳过当前列表迭代的其余部分:

for name, row in recent.iteritems():
    if name in ignore:
        print name + "is on ignore list. Skipping."
        continue

    if number_of_open_orders_in(name) == max_orders_per_market:
        print name + "has max number of open orders. Skipping."
        continue

    if row[0].ask < 100e-8:
        print name + "is a single or double satoshi coin. Skipping."
        continue

    gain.append(
        (
            name,
            percent_gain(row[0].ask, row[1].ask),
            row[1].ask,
            row[0].ask,
            'https://bittrex.com/Market/Index?MarketName={0}'.format(name),
        )
    )

如您所见,代码非常规则。跳过的每个原因

  1. 根据rowname
  2. 执行测试
  3. 如果测试返回True
    • 打印消息
    • 跳过当前循环迭代

1 个答案:

答案 0 :(得分:0)

以下是删除了冗余的代码:

for name, row in recent.iteritems():
    if name in ignore:
        print name + "is on ignore list. Skipping."

    elif number_of_open_orders_in(name) == max_orders_per_market:
        print name + "has max number of open orders. Skipping."

    elif row[0].ask < 100e-8:
        print name + "is a single or double satoshi coin. Skipping."

    else:
        gain.append(
            (
                name,
                percent_gain(row[0].ask, row[1].ask),
                row[1].ask,
                row[0].ask,
                'https://bittrex.com/Market/Index?MarketName={0}'.format(name),
            )
        )