在python中创建2D数组的两种方法

时间:2018-05-11 22:16:51

标签: python python-3.x list

以下两种在python中创建2d数组的方法有什么区别?

def arrays(row, column):
    myList = [[None]*column for i in range(row)]

def arrays(row, column):
   myList = [[None]*column]*row

1 个答案:

答案 0 :(得分:3)

在第一种情况下,单独的指针用于存储子列表。

在第二个实例中,使用相同的指针。因此,改变一个人的价值也会改变其他人。

以下是一个说明性示例: -

public class Llaves1 {

    public static int numPila;
    private int t = -1;
    private char[] pila;  Llaves1(int size){
        this.pila = new char[size];
        numPila++;
    } 

       public int size(){
        return (t+1);
    }

    public boolean isEmpty(){
        if(t==-1)
            return true;
        else
            return false;
    }

    public  void push(char x){
        if(this.size()== this.pila.length)
        {
            System.out.println("Pile is already full.");
        }
         else {
        this.pila[++t]=x;
        }
    }

    public char pop(){
        if(isEmpty()){
            System.out.println("Empty Pile.");
        }
        return pila[t--];
    }

    public char top(){
        if(isEmpty()){
            System.out.println("Empty pile.");
        }
        return pila[t];
    }

    public static void main(String[] args) {

        Scanner teclado = new Scanner(System.in);
        String cadena= teclado.nextLine();

        Llaves1 a= new Llaves1(cadena.length());
        for(int i=0; i<cadena.length();i++){
           char temp = cadena.charAt(i);
           a.push(temp);
        }

        if(a.isEmpty())
            System.out.println("Is empty.");
        int numC=0;
        int numA=0;

        for(int i=0; i<cadena.length(); i++){
            if(a.top()=='}')
               numC++;
            if(a.top()=='{')
               numA++;
            a.pop();
        }
        if(numA!=numC)
            System.out.println("You have to close curly braces");
        else 
            System.out.println("all curly braces have been closed.");
    }

}