Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在字典中将“None”值替换为0的最佳方法_Python - Fatal编程技术网

Python 在字典中将“None”值替换为0的最佳方法

Python 在字典中将“None”值替换为0的最佳方法,python,Python,我有一本字典- { 'buy': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None}, 'sell': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None} } 用0替换None值的最佳方法是什么 注意-并非每次这些键的值都是None时,您可以使用字典理解

我有一本字典-

{

    'buy': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None}, 
    'sell': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None}
}
0
替换
None
值的最佳方法是什么


注意-并非每次这些键的值都是
None

时,您可以使用字典理解方法,如果返回错误值(例如
None
False
'
0
),则将值更改为
0

d={'a':无,'b':1}
d1={k:v或0表示d.items()中的(k,v)}
#{a':0,'b':1}

您可以使用字典理解方法,如果返回错误的值(例如
None
False
'
0
),则将该值更改为
0

d={'a':无,'b':1}
d1={k:v或0表示d.items()中的(k,v)}
#{a':0,'b':1}

您可以通过检查值的类型来查看它是否是嵌套的
dict
来递归地将dict中的所有
None
替换为
0

test_dict = {
    'buy': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None},
    'sell': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None}
}

def replace_none_with(d, replacement=0):
    retval = {}
    for key, val in d.items():
        if val is None:
            retval[key] = replacement
        elif isinstance(val, dict):
            retval[key] = replace_none_with(val, replacement)
        else:
            retval[key] = val
    return retval

print(replace_none_with(test_dict))
输出:

{'buy': {'trade_transaction_amount__sum': 0, 'tax__sum': 0, 'trade_fee__sum': 0}, 'sell': {'trade_transaction_amount__sum': 0, 'tax__sum': 0, 'trade_fee__sum': 0}}

通过检查值的类型,查看是否为嵌套的
dict
,可以递归地将dict中的所有
None
替换为
0

test_dict = {
    'buy': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None},
    'sell': {'trade_transaction_amount__sum': None, 'tax__sum': None, 'trade_fee__sum': None}
}

def replace_none_with(d, replacement=0):
    retval = {}
    for key, val in d.items():
        if val is None:
            retval[key] = replacement
        elif isinstance(val, dict):
            retval[key] = replace_none_with(val, replacement)
        else:
            retval[key] = val
    return retval

print(replace_none_with(test_dict))
输出:

{'buy': {'trade_transaction_amount__sum': 0, 'tax__sum': 0, 'trade_fee__sum': 0}, 'sell': {'trade_transaction_amount__sum': 0, 'tax__sum': 0, 'trade_fee__sum': 0}}

以下是适用于任何级别嵌套字典的递归方法:

d = {
    'buy': {'trade_transaction_amount__sum': None, 'tax__sum': None,
            'trade_fee__sum': None},
    'sell': {'trade_transaction_amount__sum': None, 'tax__sum': None,
             'trade_fee__sum': None}
}


def replacer(dictionary):
    for k, v in dictionary.items():
        if isinstance(v, dict):
            replacer(v)

        elif v is None:
            dictionary[k] = 0


replacer(d)
print(d)

以下是适用于任何级别嵌套字典的递归方法:

d = {
    'buy': {'trade_transaction_amount__sum': None, 'tax__sum': None,
            'trade_fee__sum': None},
    'sell': {'trade_transaction_amount__sum': None, 'tax__sum': None,
             'trade_fee__sum': None}
}


def replacer(dictionary):
    for k, v in dictionary.items():
        if isinstance(v, dict):
            replacer(v)

        elif v is None:
            dictionary[k] = 0


replacer(d)
print(d)

到目前为止你试过什么?你说它们不总是没有是什么意思?它们也是空字符串“”还是NaN?或者有时它们有一个值?它们有一个整数值或者没有。到目前为止,你试过什么?你说它们不总是没有是什么意思?它们也是空字符串“”还是NaN?或者有时它们有一个值?它们有一个整数值,或者没有。