翻转零和一在一维numpy的阵列 [英] Flipping zeroes and ones in one-dimensional NumPy array

查看:100
本文介绍了翻转零和一在一维numpy的阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个一维数组numpy的,由零和一像这样的:

I have a one-dimensional NumPy array that consists of zeroes and ones like so:

array([0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1])

我想一个快速的方法只是翻转的值,使得零成为的人,那些和零变,导致numpy的数组是这样的:

I'd like a quick way to just "flip" the values such that zeroes become ones, and ones become zeroes, resulting in a NumPy array like this:

array([1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])

有一个简单的单行这个?我看了看 fliplr()的功能,但是这似乎需要的尺寸大于或等于2的numpy的阵列。我敢肯定有一个相当简单的答案,但任何帮助,将AP preciated。

Is there an easy one-liner for this? I looked at the fliplr() function, but this seems to require NumPy arrays of dimensions two or greater. I'm sure there's a fairly simple answer, but any help would be appreciated.

推荐答案

一定有什么东西在您的问与答,我不明白...

There must be something in your Q that i do not understand...

反正

In [2]: from numpy import array

In [3]: a = array((1,0,0,1,1,0,0))

In [4]: b = 1-a

In [5]: print a ; print b
[1 0 0 1 1 0 0]
[0 1 1 0 0 1 1]

In [6]: 

这篇关于翻转零和一在一维numpy的阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆