简体   繁体   English

为什么它跳过数组输入的第一行

[英]why does it skips the first line for input of an array

Input Format输入格式

The first line contains the space separated values of N and M. The next lines contain the space-separated elements of columns.第一行包含 N 和 M 的空格分隔值。下一行包含列的空格分隔元素。

input
2 2
1 2
3 4

the code编码

import numpy


my_array = numpy.array([input().split() for _ in range(int(input().split()[0]))],int)

print(my_array.T,my_array.flatten(),sep = "\n")

while taking input for the array how the code skips the first line "2 2" containing number of rows and number of columns, i just want an understanding of how it starts taking input from the second line在为数组获取输入时,代码如何跳过包含行数和列数的第一行“2 2”,我只想了解它如何开始从第二行获取输入

output
[[1 3]
 [2 4]]
[1 2 3 4]

This code uses list comprehension, in which the call of the latter input() reads the first line, so that it takes input from the second line.此代码使用列表理解,其中后一个input()的调用读取第一行,以便从第二行获取输入。

This code is equivalent to:这段代码相当于:

import numpy as np

# Read the first line.
num_rows = int(input().split()[0])

# Deal with data.
rows = []
for __ in range(num_rows):
    rows.append(input().split())
my_array = np.array(rows, int)

print(my_array.T, my_array.flatten(), sep='\n')

First, the for _ in range(int(input().split()[0])) gets executed and the first line (in your sample input, 2 2 ) is read from the input.首先,执行for _ in range(int(input().split()[0]))并从输入中读取第一行(在您的示例输入中, 2 2 )。 Then for the range obtained from the executed range(int(input().split()[0])) the next lines would be read.然后对于从执行的range(int(input().split()[0]))获得的范围,将读取下一行。 In your sample input, first the for _ in range(int(input().split()[0])) will be executed, which is actually for _ in range(2)) and then the 2 next lines will be read with input().split() .在您的示例输入中,首先将执行for _ in range(int(input().split()[0])) ,这实际上是for _ in range(2)) ,然后将读取下2行与input().split()

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

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