将列附加到2D数组的开头和结尾

时间:2018-06-15 19:14:37

标签: python arrays numpy append

我想带两个1D阵列

Ne_initial = Ne[:,0]
Ne_final = Ne[:,-1]

将它们作为第一行(Ne_initial)和最后一行(Ne_final)附加到现有的2D数组中。我不确定该怎么做。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:0)

您可以使用此代码:

<!DOCTYPE html>
<html>
  <head>
    <link rel="stylesheet" type="text/css" href="style.css">
    <title>Etch-a-sketch</title>
  </head>
  <body>
    <h1>Etch-a-sketch</h1>
    <button id="start">Start</button>
    <div id="container"></div>
  </body>
  <script>


  //randomColor function is taken from http://www.devcurry.com/2010/08/generate-random-colors-using-javascript.html //
  function randomRgb(value) {

  col =  "rgb("
  + randomColor(255) * value + ","
  + randomColor(255) * value + ","
  + randomColor(255) * value + ")";
  }

  function randomColor(num) {
    return Math.floor(Math.random() * num);
  }

  function resetColorOfBoxes() {
    boxes = document.querySelectorAll('div');
    boxes.forEach(box => box.style.backgroundColor = "white");
  }

  function promptEntry() {

    let userInput = prompt("How many rows would you like?", "Enter a number");

    if (isNaN(userInput)) {
      alert("That's not a valid entry. Try again");
      promptEntry();
    }

    else {
      createGrid(userInput);
    }
  }

  function createGrid(numberOfRows) {

    resetColorOfBoxes()

    let gridTemplateColumns = 'repeat('+numberOfRows+', 1fr)'

    var container = document.getElementById("container");
    container.style.gridTemplateColumns = gridTemplateColumns;
    container.style.gridTemplateRows = gridTemplateColumns;

    let brightness = [];

    let i = 0;
    let numberOfBoxes = numberOfRows**2;

    /*Create boxes*/
    for (i; i < numberOfBoxes ; i++) {
      brightness[i+1] = 1;
      console.log(brightness);

      var div = document.createElement("div");
      div.classList.add(i+1);
      document.getElementById("container").appendChild(div);

      div.addEventListener("mouseenter", function () {
        var className = this.className;
        brightness[className] = brightness[className] - 0.1;
        console.log(brightness[className]);
        randomRgb(brightness[className]);
        this.style.backgroundColor = col;
      });
    }
  }

  let btn = document.getElementById("start")
  btn.addEventListener("click", promptEntry)

  </script>
</html>

答案 1 :(得分:0)

以下示例应该可以实现您的目标:

在:

import numpy as np

existing2Darray = np.matrix([[8, 3, 6, 1],[2, 5, 4, 2],[7, 2, 5, 1]])
Ne_initial = existing2Darray[:,0]
Ne_last = existing2Darray[:,-1]

OUT:

 [[8 3 6 1]
 [2 5 4 2]
 [7 2 5 1]]

[[8]
 [2]
 [7]]

[[1]
 [2]
 [1]]

在:

np.append(existing2Darray, Ne_initial, axis=1))

OUT:

[[8 3 6 1 8]
 [2 5 4 2 2]
 [7 2 5 1 7]]

在:

np.insert(existing2Darray, [0], Ne_last, axis=1))

OUT:

[[1 8 3 6 1]
 [2 2 5 4 2]
 [1 7 2 5 1]]

答案 2 :(得分:0)

import numpy as np 
a = np.array([[1,2],[3,4]]) 

print 'First array:' 
print a 
print '\n'  
b = np.array([[5,6],[7,8]]) 

print 'Second array:' 
print b 
print '\n'

print 'Vertical stacking:' 
c = np.vstack((a,b)) 
print c # creating a 2-D array
col1=np.array([2,3,4,5])
col2=np.array([1,1,1,1])
d=np.column_stack((a1,b2))
np.concatenate((d,c)) # adding back to existing array