在Google Colab中显示所有带有换行符的输出

时间:2018-10-13 16:22:05

标签: jupyter-notebook jupyter google-colaboratory

我正在尝试显示我的Google Colab Notebook中一个单元格的所有输出。

我找到了Jupyter Notebooks的设置,该设置显示所有输出,而不仅仅是最后一行:

   public class State {

    public double id;
    public String[][] matrix;

    public State() {

    }

    public State(String[][] matrix) {
        this.matrix = createMatrix(matrix);//is created from a existing matrix
        this.id = generateId();
    }

    @Override
    public boolean equals(Object other) {
        if ((other == null) || !(other instanceof State)) {
            return false;
        }
        return ((State) other).getId().equals(this.getId()) && ((State) other).getId() == this.getId();
    }

    @Override
    public int hashCode() {
        long bits = doubleToLongBits(id);
        int hash = (int) (bits ^ (bits >>> 32));
        System.out.println("hash: " + hash);
        return hash;
    }

    public String toString() {
        return "Hashcode: " + this.hashCode();
    }

    public Double getId() {
        return id;
    }

    public void setId(Double id) {
        this.id = id;
    }

    public String[][] getMatrix() {
        return matrix;
    }

    public void setMatrix(String[][] matrix) {
        this.matrix = matrix;
    }

    public double generateId() {
        String sid = "";
        for (int i = 0; i < this.matrix[0].length; i++) {
            for (int j = 0; j < matrix[1].length; j++) {
                if (matrix[i][j].equals("0")) {
                    sid += ".";
                } else {
                    sid += matrix[i][j];
                }
            }
        }
        System.out.println("ID: " + new BigDecimal(sid).doubleValue());
        return new BigDecimal(sid).doubleValue();

    }

    private String[][] createMatrix(String[][] matriz) {
        String[][] copia = new String[matriz[0].length][matriz[1].length];
        for (int i = 0; i < copia[0].length; i++) {
            for (int j = 0; j < copia[1].length; j++) {
                copia[i][j] = matriz[i][j];
            }
        }
        return copia;
    }

但是,输出没有被换行符分隔,这引起类似于以下问题的问题

from IPython.core.interactiveshell import InteractiveShell

InteractiveShell.ast_node_interactivity = "all"

所需的输出将更像:

in: 
a = 3
a 
a+1 

out:
34

如何使所有不同的输出以换行符分隔? Jupyter Notebooks / Google Colab中可能吗?

1 个答案:

答案 0 :(得分:2)

您可以尝试在两者之间添加打印件

a=3
a
print()
a+1

或者简单地打印两行

a=3
print(a)
print(a+1)