从字符串数组中删除nan

时间:2018-11-12 16:19:48

标签: python arrays python-2.7 list numpy

我有以下数组:

public class MainActivity extends AppCompatActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final Button button = findViewById(R.id.button);
        final Button button2 = findViewById(R.id.button2);




        button.setOnClickListener(new View.OnClickListener() {
            @Override

            //button do uruchomienia roma
            public void onClick(View v) {

                Thread thread = new Thread(new Runnable() {

                    public void run() {
                        try {

                            executeRemoteCommand("pi", "123","192.168.0.115", 22,"sudo python3 UruchomienieRoma.py");

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }



       });
thread.start();

    }
    });


        button2.setOnClickListener(new View.OnClickListener() {
            @Override

            //button do uruchomienia roma
            public void onClick(View v) {

                Thread thread2 = new Thread(new Runnable() {

                    public void run() {
                        try {

                            executeRemoteCommand("pi", "123","192.168.0.115", 22,"sudo python3 ESC.py " );

                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }



                });
                thread2.start();

            }
        });




    }

    public  String executeRemoteCommand(String username,String password,String hostname,int port ,String command)
            throws Exception {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, hostname, port);
        session.setPassword(password);

        // Avoid asking for key confirmation
        Properties prop = new Properties();
        prop.put("StrictHostKeyChecking", "no");
        session.setConfig(prop);

        session.connect();

        // SSH Channel
        ChannelExec channelssh = (ChannelExec)
                session.openChannel("exec");
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        channelssh.setOutputStream(baos);

        // Execute command
        channelssh.setCommand(command);
        channelssh.connect();
        channelssh.disconnect();

         return baos.toString();
    }
}

如何删除nan来获取:

data=array([['beef', 'bread', 'cane_molasses', nan, nan, nan],
       ['brassica', 'butter', 'cardamom']])

我已经尝试过here中给出的方法,但是这种方法不起作用,因为在我的情况下,我的数组具有较高的维数,并且不是简单的向量。

1 个答案:

答案 0 :(得分:1)

object dtype数组不支持向量化操作。但是您可以先进行往返转换,再转换为list,然后再转换回数组。在这里,我们使用事实np.nan != np.nan by design

data = np.array([['beef', 'bread', 'cane_molasses', np.nan, np.nan, np.nan],
                 ['brassica', 'butter', 'cardamom']])

res = np.array([[i for i in row if i == i] for row in data.tolist()])

array([['beef', 'bread', 'cane_molasses'],
       ['brassica', 'butter', 'cardamom']], 
      dtype='<U13')

请注意,结果数组将为字符串类型(此处最大长度为13)。如果要使用object dtype数组,该数组可以容纳任意对象,则需要指定dtype=object

res = np.array([[i for i in row if i == i] for row in data.tolist()], dtype=object)

array([['beef', 'bread', 'cane_molasses'],
       ['brassica', 'butter', 'cardamom']], dtype=object)