首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >python字典清除不可迭代

python字典清除不可迭代
EN

Stack Overflow用户
提问于 2018-06-16 22:53:19
回答 2查看 63关注 0票数 -1

我是Python的新手,希望有人能帮助我,我试过了,但我得到了一个错误: TypeError:'NoneType‘类型的参数不可迭代我认为问题是一些值是无的,我不能删除没有值的东西?

etheurticker= finexticker.get('ETH/EUR')
if 'askVolume' and 'bidVolume' and 'vwap' and 'open' and 'previousClose' and 
'change' and 'percentage' and 'baseVolume' and 'quoteVolume' and 'info'and 
'average' in etheurticker:
        del etheurticker['askVolume']
        del etheurticker['bidVolume']
        del etheurticker['vwap']
        del etheurticker['open']
        del etheurticker['previousClose']
        del etheurticker['change']
        del etheurticker['percentage']
        del etheurticker['baseVolume']
        del etheurticker['quoteVolume']
        del etheurticker['info']
        del etheurticker['average']

这是没有清除的字典:

    {'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 'datetime': '2018-06- 
   16T14:49:01.908Z', 'high': 440.45, 'low': 416.57356472, 'bid': 422.75, 
    'bidVolume': None, 'ask': 422.77, 'askVolume': None, 'vwap': None, 'open':None, 'close': 422.76, 'last': 422.76, 'previousClose': None, 'change': None, 'percentage': None, 'average': 422.76, 'baseVolume': 3318.1630349899992, 'quoteVolume': None, 'info': {'mid': '422.76', 'bid': '422.75', 'ask': '422.77', 'last_price': '422.76', 'low': '416.57356472', 'high': '440.45', 'volume': '3318.1630349899992', 'timestamp': '1529160540.908208614', 'pair': 'ETHEUR'}}

希望任何人能帮助我:)

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-06-16 23:15:30

您可以像这样删除它们:

etheurticker = {'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 
                'datetime': '2018-06-16T14:49:01.908Z', 
                'high': 440.45, 'low': 416.57356472, 'bid': 422.75, 
                'bidVolume': None, 'ask': 422.77, 'askVolume': None, 'vwap': None, 
                'open':None, 'close': 422.76, 'last': 422.76, 'previousClose': None, 
                'change': None, 'percentage': None, 'average': 422.76, 
                'baseVolume': 3318.1630349899992, 'quoteVolume': None, 
                'info': {'mid': '422.76', 'bid': '422.75', 'ask': '422.77', 
                         'last_price': '422.76', 'low': '416.57356472', 'high': '440.45', 
                         'volume':'3318.1630349899992', 'timestamp':'1529160540.908208614', 
                         'pair': 'ETHEUR'}}


print(etheurticker)

print("\nafter:\n")

# all things that must be in the dict so you delete them:
data =  ['askVolume' ,'bidVolume' ,'vwap' , 'open' ,'previousClose', 'change', 
         'percentage', 'baseVolume','quoteVolume','info', 'average']

# check if all are given
if all(k in etheurticker for k in data):
    for n in data : # iterate names to delete them
        del etheurticker[n] 

print(etheurticker)

输出:

{'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 'datetime': '2018-06-16T14:49:01.908Z', 
 'high': 440.45, 'low': 416.57356472, 'bid': 422.75, 'bidVolume': None, 
 'ask': 422.77, 'askVolume': None, 'vwap': None, 'open': None, 'close': 422.76, 
 'last': 422.76, 'previousClose': None, 'change': None, 'percentage': None, 
 'average': 422.76, 'baseVolume': 3318.1630349899992, 'quoteVolume': None, 
 'info': {'mid': '422.76', 'bid': '422.75', 'ask': '422.77', 'last_price': '422.76', 'low': '416.57356472', 'high': '440.45', 'volume': '3318.1630349899992', 'timestamp': '1529160540.908208614', 'pair': 'ETHEUR'}}

after:

{'symbol': 'ETH/EUR', 'timestamp': 1529160540908.2085, 'datetime': '2018-06-16T14:49:01.908Z', 
 'high': 440.45, 'low': 416.57356472, 'bid': 422.75, 'ask': 422.77, 
 'close': 422.76, 'last': 422.76}
票数 2
EN

Stack Overflow用户

发布于 2018-06-16 23:10:54

不考虑你为什么要这样做,下面是你实际如何设置条件(用一个较小的例子):

etheurticker = {
    'askVolume': 1,
    'bidVolume': 2,
    'vwap': 3
}

if all(x in etheurticker for x in ['askVolume', 'bidVolume', 'vwap']):
    print("they are all there")
    del etheurticker['vwap']
    print(etheurticker)
else:
    print("not all there")

# they are all there
# {'askVolume': 1, 'bidVolume': 2}

请注意,将vwap设置为None并不重要

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50888967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档