【问题标题】:Error cannot unpack non-iterable when trying to Iterate over OrderedDict()尝试迭代 OrderedDict() 时,错误无法解压不可迭代
【发布时间】:2021-01-30 14:12:59
【问题描述】:

我正在尝试在 python 3.9 上迭代 OrderedDict,但是当我得到:

对于键,d 中的值: TypeError: cannot unpack non-iterable int object execute the for bucle

这是我的代码:

MAXSIZE = 5
d = dict()

while True:

    candles = api.get_candles(EURUSD,5,1,time.time())

    candleOpen = float(candles[0]['open'])
    candleClose = float(candles[0]['close'])

    candleID = candles[0]['id']
                
    if candleOpen < candleClose:            
        d2 = {candleID: 'A'}

    elif candleOpen > candleClose:          
        d2 = {candleID: 'B'}
        
    else:
        d2 = {candleID: 'C'}            
    
    if len(d) == MAXSIZE:
        for key, value in d:
            print(key)

【问题讨论】:

    标签: python loops for-loop ordereddictionary iterable-unpacking


    【解决方案1】:

    要遍历字典的对,请使用.items()

    # print(type(d)) to ensure you have a dict
    for key, value in d.items():
        print(key)
    

    另外,您似乎有一个字典 d,但随后您创建了另一个字典 d2,检查一下;)

    【讨论】:

      猜你喜欢
      • 2020-07-12
      • 2022-11-15
      • 2018-02-10
      • 2020-01-28
      • 2019-04-22
      • 1970-01-01
      • 2020-01-18
      • 2011-08-09
      • 1970-01-01
      相关资源
      最近更新 更多