我正在尝试将包含10个值的多个列表压缩在一起。列表由迭代器创建。有时,生成的列表包含少于10个值甚至0个值。因此,我有时会遇到这样的问题:尝试将10个值的列表与0值列表压缩在一起,或者甚至将0值列表与0值的另一个列表压缩在一起。我试图让python识别一个0值的列表,然后用0填充该列表。这就是我所拥有的(第二个URL是问题):
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
。我似乎无法弄清楚为什么空数组没有像我更简单的例子那样填充零。实际上,任何使用tupled零列表填充空数组的方法都适用于我。如果不清楚,我很抱歉,我对编码很陌生,而且我在这个项目上花了相当多的时间,觉得我迷失在杂草中。任何帮助表示赞赏。
注意:这个问题与我在How do I get my DataNitro table to either skip over failed iterations or print none to the table?的其他问题直接相关,但我觉得这两个问题尽管有着共同点,但却截然不同。
答案 0 :(得分:1)
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)