【问题标题】:TypeError: zip argument #1 must support iterationTypeError: zip 参数 #1 必须支持迭代
【发布时间】:2012-12-12 13:24:54
【问题描述】:
for k,v in targets.iteritems():
    price= str(v['stockprice'])

    Bids = str(u''.join(v['OtherBids']))
    Bids = Bids.split(',')

    # create a list of unique bids by ranking
    for a, b in zip(float(price), Bids):
        try:
            del b[next(i for i, e in enumerate(b) if format(e, '.4f')  == a)]
        except StopIteration:
            pass

我正在从我的字典中提取数据,但它们似乎都是 unicode。我怎样才能摆脱 unicode 的废话?

【问题讨论】:

  • 你能分享字典(目标)的键和值吗??
  • 您的问题是什么?您的主题标题是 TypeError: zip argument #1 must support iteration。您的代码是否给您此错误消息?您的问题是关于解决该错误消息吗?或者你的问题是关于摆脱 Unicode “废话”?
  • 吉姆,谢谢。我认为 unicode 是问题所在。

标签: python python-2.7 iterator typeerror python-zip


【解决方案1】:

我认为您的代码给了您错误消息,TypeError: zip argument #1 must support iteration。由于表达式zip(float(price), Bids),您会收到此错误。这个简化的代码演示了错误:

>>> price = str(u'12.3456')
>>> bids = ['1.0', '2.0']
>>> zip(float(price), bids)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: zip argument #1 must support iteration

Python 2.x zip() built-in library function 要求其所有参数都是可迭代的。 float(price) 不是可迭代的。

如果您想创建将float(price) 与数组Bids 的每个元素组合在一起的元组,您可以在第一个参数表达式中使用itertools.repeat()

>>> import itertools
>>> price = str(u'12.3456')
>>> bids = ['1.0', '2.0']
>>> zip(itertools.repeat(float(price),len(bids)), bids)
[(12.345599999999999, '1.0'), (12.345599999999999, '2.0')]

我认为您对 Unicode 数据类型的使用与 TypeError 没有任何关系。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-10-27
    • 1970-01-01
    • 1970-01-01
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多