问题标签 [map-function]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
3 回答
9286 浏览

python - 用零替换python数组中的无

我刚刚将两个长度不等的数组与命令一起加入:

在今天的订单没有值的地方给出“无”(因为今天的订单数组没有那么长)。

但是,当我尝试将 allorders 数组传递到 matplotlib 条形图中时:

..我收到以下错误:

那么,matplotlib 有没有办法不接受任何数据类型?如果没有,如何在我的 allorders 数组中用零替换“Nones”?

如果可以,因为我是 Python 新手(来自 R 社区),请提供从头到尾的详细代码,我可以使用/测试。

0 投票
39 回答
1938133 浏览

javascript - 对象的映射函数(而不是数组)

我有一个对象:

我正在寻找一种本机方法,类似于Array.prototype.map将使用如下:

JavaScriptmap对对象有这样的功能吗?(我想要这个用于 Node.JS,所以我不关心跨浏览器问题。)

0 投票
3 回答
484 浏览

python - 映射函数和输入函数参数

这是一个愚蠢的例子:

当我将其作为输入函数时,x如何指定 的参数?add_x_to_inputmap

0 投票
4 回答
3064 浏览

python - Python : Map function with none type list as parameter

I want to pass a list of None in a map function but it doesn't work.

I have this message error:

0 投票
4 回答
17878 浏览

python - Python `map` 和参数解包

我知道

相当于

是否可以使用地图功能执行以下操作?

0 投票
2 回答
560 浏览

scheme - 方案中的 n arity zip 函数(应用和映射问题)

我在列表列表上调用 map 时遇到问题。

这就是我们想要的,但我无法通过可调用函数来实现它。

编辑——(map (lambda x x) '(1 2) '(3 4) '(5 6))给出相同的结果!

我可以看到传递给 zip 的参数变成一个列表的问题,但我不知道继续像对待工作版本一样对待它们。

我不确定如何apply与地图一起使用

这是有道理的,因为 map 没有调用任何东西。;; 但是我们如何将 map 应用于 map 调用 fold 函数的相同参数?

这与第一次尝试相同。我确定(...希望)我在这里遗漏了一些小东西。

0 投票
1 回答
901 浏览

scala - 对 Scala 中的“提升”功能感到困惑

在Scala 中的函数式编程一书中,有一个“提升”的例子,其中一个类型的函数A => B被提升为Option[A] => Option[B].

这是电梯的实现方式:

我对此有一些困惑:

第一个是,这里的“_”是什么?其次,当我从 def 中删除返回类型时,期望类型推断发挥其魔力,我得到以下异常:

有人可以解释这里发生了什么吗?

谢谢

0 投票
3 回答
61 浏览

python - “压缩”序列中的任意序列(实际上是映射序列)

这很好用:

(

)

但是,如果我想要列表而不是元组,它会失败:

我可以通过以下方式解决这个问题:

但是有没有我可以直接在 map() 中使用的东西?

似乎大多数序列都有这个问题:

我希望能够将一个特殊的序列传递给 map() 并获取这些序列的列表(例如列表、集合等),并结合来自“压缩”(映射)序列的后续元素。

0 投票
3 回答
583 浏览

functional-programming - 不从 LISP/Scheme 返回任何东西

基本上,我想使用 map 在列表中进行选择,例如

我想要的结果是

我尝试将 else 部分留空,它抱怨需要 else 部分。我试过了

作为其他部分并得到

那很接近。

有什么方法可以使用地图来获取'(c)?我知道递归方式,但我想知道 map 是否也可以这样做。如果不是 '(c),至少 (##c#) 但不使用 display hack 来实现 void 类型的返回值。


0 投票
2 回答
9496 浏览

python - multiprocessing pool.map call functions in certain order

How can I make multiprocessing.pool.map distribute processes in numerical order?


More Info:
I have a program which processes a few thousand data files, making a plot of each one. I'm using a multiprocessing.pool.map to distribute each file to a processor and it works great. Sometimes this takes a long time, and it would be nice to look at the output images as the program is running. This would be a lot easier if the map process distributed the snapshots in order; instead, for the particular run I just executed, the first 8 snapshots analyzed were: 0, 78, 156, 234, 312, 390, 468, 546. Is there a way to make it distribute them more closely to in numerical order?


Example:
Here's a sample code which contains the same key elements, and show's the same basic result:

Yields:


Answer:

From @Hayden: Use the 'chunksize' parameter, def map(self, func, iterable, chunksize=None).

More Info:
The chunksize determines how many iterations are allocated to each processor at a time. My example above, for instance, uses a chunksize of 2---which means that each processor goes off and does its thing for 2 iterations of the function, then comes back for more ('check-in'). The trade-off behind chunksize is that there is overhead for the 'check-in' when the processor has to sync up with the others---suggesting you want a large chunksize. On the other hand, if you have large chunks, then one processor might finish its chunk while another-one has a long time left to go---so you should use a small chunksize. I guess the additional useful information is how much range there is, in how long each function call can take. If they really should all take the same amount of time - it's way more efficient to use a large chunk size. On the other hand, if some function calls could take twice as long as others, you want a small chunksize so that processors aren't caught waiting.

For my problem, every function call should take very close to the same amount of time (I think), so if I want the processes to be called in order, I'm going to sacrifice efficiency because of the check-in overhead.