代码之家  ›  专栏  ›  技术社区  ›  Jim Carlson

当随机列表返回空时,如何使用itertools和fillvalue压缩生成的列表?

  •  2
  • Jim Carlson  · 技术社区  · 7 年前

    import grequests
    import json
    import time
    import itertools
    
    urls3 = [
            #'https://api.livecoin.net/exchange/order_book?currencyPair=RBIES/BTC&depth=5',
            'https://api.livecoin.net/exchange/order_book?currencyPair=REE/BTC&depth=5',
            #'https://api.livecoin.net/exchange/order_book?currencyPair=RLT/BTC&depth=5',
    ]
    requests = (grequests.get(u) for u in urls3)
    responses = grequests.map(requests)
    #CellRange("B28:DJ48").clear()
    def make_column(catalog_response, name):
            column = []
            catalog1 = list(itertools.izip_longest(catalog_response.json()[name][0:5], fillvalue='0 '))
            #catalog1 = catalog_response.json()[name][0:5]
            print(catalog1)
            #quantities1, rates1 = list(itertools.izip_longest(*catalog1,fillvalue='0.0001'))    #uncomment for print #2
            #quantities1, rates1 = zip(*catalog1)    #uncomment for print #2
            print(quantities1)
    

    打印输出 catalog1 因为只有第二个链接会产生以下输出:

    []
    [([u'0.00000001', u'9907729.00000000'],), ([u'0.00000001', u'44800.00000000'],), ([u'0.00000002', u'8463566.49169284'],), ([u'0.00000002', u'3185222.59932121'],), ([u'0.00000002', u'25000.00000000'],)]
    

    如您所见,第一个阵列打印 []

    import itertools
    
    list1 = ['a', 'b', 'c', 'd', 'e']
    list2 = []
    
    print list(itertools.izip_longest(list1,list2, fillvalue='0'))
    

    该输出如下:

    [('a', '0'), ('b', '0'), ('c', '0'), ('d', '0'), ('e', '0')]

    我想也许是跑步

        `column = []
        catalog1 = list(itertools.izip_longest(catalog_response.json()[name][0:5], fillvalue='0 '))
        #catalog1 = catalog_response.json()[name][0:5]
        #print(catalog1)
        quantities1, rates1 = list(itertools.izip_longest(*catalog1,fillvalue='0'))    #uncomment for print #2
        #quantities1, rates1 = zip(*catalog1)    #uncomment for print #2
        print(quantities1)`
    

    可能会解决问题。但它返回以下错误: ValueError: need more than 0 values to unpack

    How do I get my DataNitro table to either skip over failed iterations or print none to the table? 但我觉得这两个问题虽然有着相同的目的,但却是截然不同的。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Carl Sandburg    7 年前
    lst2 = ([[u'0',u'0'],[u'0',u'0'],[u'0',u'0'],[u'0',u'0'],[u'0',u'0']])
    catalog1 = catalog_response.json()[name][0:5]
    S = catalog1 + lst2
    quantities1, rates1 = zip(*S)