python字典清除不是iterab

2024-09-30 12:21:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python新手,希望有人能帮我 我尝试了这个,但是我得到了一个错误:TypeError:NoneType类型的参数不可iterable 我想问题是有些值是无的,我不能删除一些无的?你知道吗

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'}}

希望有人能帮我:)


Tags: andinfononeopenchangeaveragedelpercentage
2条回答

您可以这样删除它们:

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}

不管您试图解释为什么要这样做,下面是您实际如何设置该条件(使用一个较小的示例):

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并不重要

相关问题 更多 >

    热门问题