python - 应用 map 函数时在 python 中投影变量

标签 python dictionary functional-programming

在 Python 中映射一组项目时,是否可以投影内部函数的第二个参数?

当使用 map 和 int cast 函数的 monadic 使用时,它的工作原理很简单:

list(map(int, ["4","3","1","2"]))

但我想使用 int 的二元形式 int("1101",2) 使用 map 将多个二进制数字转换为 int:

list(map(int(,2), ["1101","0001","1100","0011"]))

最佳答案

你需要lambda

list(map(lambda x : int(x,2), ["1101","0001","1100","0011"]))

但是当您需要 listlambdamap 时,这是一个毫无意义的赠品,您只需要列表理解:

[int(x,2) for x in ["1101","0001","1100","0011"]]

(当需要 lambda 时,比 map 更清晰、更短、更快)

关于python - 应用 map 函数时在 python 中投影变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45980275/

相关文章:

haskell - 在 Haskell 中求解方程

Python 2.7 - 在一次执行中从字典键值插入 MySql 中的多个值

ios - 使用 Firebase 异步字典下载创建对象数组 (Swift)

python - 使用 argparse 创建相互包含的位置参数

python - Pickle 忽略带槽的卡住数据类上的 __getstate__

python - 具有多个条件的 Pandas 映射

multithreading - 为什么使用 Iterator::map 生成线程不能并行运行线程?

haskell - 如何减少 Haskell 应用程序中的内存使用量?

python - Python 进程和线程如何映射到硬件线程?

python - 在 3D 空间中插值标量场