Advertisement
Guest User

Untitled

a guest
Dec 31st, 2012
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. src_array[:, 3] += column_array_to_add[:, 0]
  2.  
  3. import numpy
  4. src = numpy.array([["a", "b"], ["c", "d"], ["e", "f"]])
  5. src2 = numpy.array([["x"], ["y"], ["z"]])
  6.  
  7. src[:, 1] += src2[:, 0]
  8. print src
  9. exit()
  10.  
  11. src[:, 1] += src2[:, 0]
  12.  
  13. import numpy as np
  14.  
  15. x = np.array([[1,2],[3,4]])
  16.  
  17. y = np.array([[5,6],[7,8]])
  18.  
  19. >>> x
  20. array([[1, 2],
  21. [3, 4]])
  22. >>> y
  23. array([[5, 6],
  24. [7, 8]])
  25. >>> x[:,1] + y[:,1]
  26. array([ 8, 12])
  27. >>> x[:, 1] += y[:, 1] # using +=
  28. >>> x[:, 1]
  29. array([ 8, 12])
  30.  
  31. src = np.array([["a", "b"], ["c", "d"], ["e", "f"]], dtype='|S8')
  32. src2 = np.array([["x"], ["y"], ["z"]], dtype='|S8')
  33.  
  34. def add_columns(x, y):
  35. return [a + b for a,b in zip(x, y)]
  36.  
  37. def update_array(source_array, col_num, add_col):
  38. temp_col = add_columns(source_array[:, col_num], add_col)
  39. source_array[:, col_num] = temp_col
  40. return source_array
  41.  
  42. >>> update_array(src, 1, src2[:,0])
  43. array([['a', 'bx'],
  44. ['c', 'dy'],
  45. ['e', 'fz']],
  46. dtype='|S8')
  47.  
  48. >>> src[:, 1]
  49. array(['b', 'd', 'f'], dtype='|S1')
  50. >>> src[:, 1] = ['x', 'y', 'z']
  51. >>> src
  52. >>> array([['a', 'x'], ['c', 'y'], ['e', 'z']], dtype='|S1')
  53.  
  54. >>> src + src2
  55. TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'
  56.  
  57. >>> s1, s2 = np.array('a'), np.array('b')
  58. >>> s1 + s2
  59. TypeError: unsupported operand type(s) for +: 'numpy.ndarray' and 'numpy.ndarray'
  60.  
  61. >>> n1, n2 = np.array(1), np.array(2)
  62. >>> n1 + n2
  63. 3
  64.  
  65. >>> m1 = np.array([[1,2], [3,4], [5,6]])
  66. >>> m2 = np.array([[7], [8], [9]])
  67. >>> m1[:, 1] += m2[:, 0]
  68. >>> array([[ 1, 9],
  69. [ 3, 12],
  70. [ 5, 15]])
  71.  
  72. >>> src = numpy.array([["a", "b"], ["c", "d"], ["e", "f"]])
  73. >>> src
  74. array([['a', 'b'], ['c', 'd'], ['e', 'f']], dtype='|S1')
  75.  
  76. >>> src = numpy.array([["a", "b"], ["c", "d"], ["e", "f"]], dtype=object)
  77. >>> src2 = numpy.array([["x"], ["y"], ["z"]], dtype=object)
  78. >>> src[:, 1] += src2[:, 0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement