简体   繁体   English

如何将 arguments 传递给 python 中的映射器 function?

[英]How to pass arguments to mapper function in python?

i have a dictionary as follows,我有一本字典如下,

ex_dict={ 'age':[56.96078431372548, 56.4, 3.072112285489562, 55.1, 58.1, 3.0,0.3041844431225945], 'weight':[92.68823529411763, 92.85, 3.5100777634435665, 90.7, 94.75, 4.049999999999997, 0.3475494873130708]}

I would like to round off values of each key values as showed in below code我想四舍五入每个键值的值,如下面的代码所示

{k:list(map(np.round,v)) for k,v in ex_dict.items()}   

it gives an output as,它给出了 output 作为,

{'age': [57.0, 56.0, 3.0, 55.0, 58.0, 3.0, 0.0], 'weight': [93.0, 93.0, 4.0, 91.0, 95.0, 4.0, 0.0]} {'年龄': [57.0, 56.0, 3.0, 55.0, 58.0, 3.0, 0.0], '体重': [93.0, 93.0, 4.0, 91.0, 95.0, 4.0, 0.0]}

Here how should I pass an argument (decimals to be 2) to np.round function that is given in map?.在这里,我应该如何将参数(小数为 2)传递给在 map 中给出的 np.round function?

You can directly use np.round() on your list, without using the map() function.您可以直接在列表中使用np.round() ,而无需使用map() function。

Change list(map(np.round,v)) to simply np.round(v, decimals=2) . list(map(np.round,v))更改为简单np.round(v, decimals=2)

np.round() can be applied on array-like data, so it can be applied on the entire list instead of element-by-element. np.round()可以应用于类似数组的数据,因此它可以应用于整个列表而不是逐个元素。

Try this:尝试这个:

import numpy as np

ex_dict={ 'age':[56.96078431372548, 56.4, 3.072112285489562, 55.1, 58.1, 3.0,0.3041844431225945], 'weight':[92.68823529411763, 92.85, 3.5100777634435665, 90.7, 94.75, 4.049999999999997, 0.3475494873130708]}

print({k:list(map(lambda x: np.round(x,2),v)) for k,v in ex_dict.items()})

Output: Output:

{'age': [56.96, 56.4, 3.07, 55.1, 58.1, 3.0, 0.3], 'weight': [92.69, 92.85, 3.51, 90.7, 94.75, 4.05, 0.35]}
import numpy as np
ex_dict={ 'age':[56.96078431372548, 56.4, 3.072112285489562, 55.1, 58.1, 3.0,0.3041844431225945], 'weight':[92.68823529411763, 92.85, 3.5100777634435665, 90.7, 94.75, 4.049999999999997, 0.3475494873130708]}

def round(value):
    return np.round(value, 2) # second paramter to round value upto

b = {k:list(map(round, v)) for k,v in ex_dict.items()}
print(b)

you can define a function round which can take value provide by map function and then return a function np.round(value,2) which take first paramter value and second parameter the round off value you can define a function round which can take value provide by map function and then return a function np.round(value,2) which take first paramter value and second parameter the round off value

Like already mentioned in the answer np.round takes a list as an argument.就像答案中已经提到的那样, np.round将列表作为参数。

{k:np.round(v,2) for k,v in ex_dict.items()}
# {'age': array([56.96, 56.4 ,  3.07, 55.1 , 58.1 ,  3.  ,  0.3 ]),
#  'weight': array([92.69, 92.85,  3.51, 90.7 , 94.75,  4.05,  0.35])}

What if a function doesn't take a list as an argument.如果 function 不将列表作为参数怎么办。 For example round You can use functools.partial here.例如round你可以在这里使用functools.partial

from functools import partial

{k:list(map(partial(round,ndigits=2),v) for k,v in ex_dict.items()}

# {'age': [56.96, 56.4, 3.07, 55.1, 58.1, 3.0, 0.3],
#  'weight': [92.69, 92.85, 3.51, 90.7, 94.75, 4.05, 0.35]}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM