numpy数组与布尔数组

    技术2022-07-11  114

    numpy数组与布尔数组

    One of the annoyances of old-school JavaScript was side effects; then Array.prototype got methods like filter, map, and forEach so we didn't need to burn variables before looping over values.  I can't explain how happy I am that the JavaScript language continues to evolve.

    老式JavaScript的烦恼之一是副作用。 然后Array.prototype获得了filter , map和forEach类的方法,因此我们不需要在遍历值之前刻录变量。 我无法解释JavaScript语言的不断发展让我多么高兴。

    Every once in a while I need to filter an array by not its original value but instead a new value, so I use map:

    每过一段时间,我需要进行过滤而不是它的原始值的阵列,而是一个新的价值,所以我使用map :

    myArray.map(item => { // Do whatever processing... // If we don't care about the item, return false or null or undefined return false; });

    While I get the new values I want, sometimes if an iteration returns a result I don't want, I return null or false, which is great, but then I'm left with a bunch of useless items in the resulting array.  The next step is using filter, in which case I could do:

    当我获得所需的新值时,有时如果迭代返回了我不想要的结果,我将返回null或false,这很好,但是随后在结果数组中留下了一堆无用的项。 下一步是使用过滤器,在这种情况下,我可以这样做:

    myArray .map(item => { // ... }) // Get rid of bad values .filter(item => item);

    Since the values I don't want aren't truthy, the filter above removes those bad items.  Did you know there's a clearer way with Boolean?

    由于我不想要的值不真实,因此上面的过滤器删除了这些不良项。 您是否知道Boolean有更清晰的方法?

    myArray .map(item => { // ... }) // Get rid of bad values .filter(Boolean);

    If the value isn't truthy the item is filtered out and I'm left with only the items I want!

    如果值不正确,该项目将被过滤掉,而我只剩下我想要的项目!

    翻译自: https://davidwalsh.name/array-boolean

    numpy数组与布尔数组

    Processed: 0.030, SQL: 9