代码之家  ›  专栏  ›  技术社区  ›  fightstarr20

python-访问numpy数组项

  •  0
  • fightstarr20  · 技术社区  · 6 年前

    我正在使用 Python 这是我在很长一段时间内第一次有点迷路。我有一个 numPy 当我打印它时像这样的数组。

       [[148 362]
         [153 403]
         [163 443]
         [172 483]
         [186 521]
         [210 553]
         [239 581]
         [273 604]
         [314 611]
         [353 602]]
    

    我正在尝试从数组中获取5项,并将其保存为2个变量x和y。

    我试着用…

    print("Item 5" + numpy_array[5])
    

    但那给了我一个错误

    typeError: ufunc 'add' did not contain a loop with signature matching types dtype('S21') dtype('S21') dtype('S21')
    

    有人能帮忙吗?

    2 回复  |  直到 6 年前
        1
  •  0
  •   iGian    6 年前

    这些是其他示例:

    print("Item 5: " + str(numpy_array[5]) ) #=> Item 5: [210 553]
    
    print("Item 5: ", numpy_array[5][0], numpy_array[5][1] ) #=> Item 5:  210 553
    
    print("Item 5: ", numpy_array[5][0], "-" , numpy_array[5][1] ) #=> Item 5:  210 553
    
    print (f"Item 5: {numpy_array[5][0]}, {numpy_array[5][1]}" ) #=> Item 5: 210, 553
    
        2
  •  1
  •   Sheldore    6 年前

    假设数组存储在名为 numpy_array 就这样。因为子数组包含2个元素,所以它会将值解包为x和y

    x, y = numpy_array[5]
    print (x, y)
    # (210, 553)