简体   繁体   English

数组索引。 中间列的打印元素

[英]Array Indexing. printing elements of the middle column

hackerank array indexing The Problem description is as follows Write the function array_index which accepts three numbers n,n_row,n_col and performs the array operations given below hackerank array indexing问题描述如下 编写 function array_index 接受三个数字 n,n_row,n_col 并执行下面给出的数组操作

  1. Create a array x of shape(n_row,n_col), having first n natural numbers.创建一个形状为 (n_row,n_col) 的数组 x,具有前 n 个自然数。 2.Print elements of last row 2.打印最后一行的元素
  2. Print elements of middle column.打印中间列的元素。 4.Print elements, overlapping first two rows and last three columns My python code is as follows 4.打印元素,重叠前两行后三列我的python代码如下

import numpy as np
def array_index(n, n_row, n_col):
    
        x = np.arange(n).reshape(n_row, n_col)
        print(x[n_col])
    
if __name__ == '__main__':
        
        n = int(input())
        n_row = int(input())
        n_col = int(input())
        array_index(n, n_row, n_col)

Can someone help me complete the solution for this problem PS: Attached the image of the question for the reference enter image description here有人可以帮我完成这个问题的解决方案 PS:附上问题图片以供参考在此处输入图片描述

import numpy as np
x=np.arange(n).reshape(n_row,n_col)
print(x[-1])
y=int(n_col/2)
print(x[:,y])
print(x[0:2,-3:])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM