[英]How to take input in this form in python?
所以,我想輸入以下內容 -
3
1 2
2 4
3 4
第一行包含一個整數 n。 以下 n 行中的每一行都包含一對不同的以空格分隔的整數。 我想將第一列的輸入存儲在一個數組中,將第二列的輸入存儲在另一個數組中。 我想出了這個代碼,你能告訴我哪里出錯了,怎么做嗎?
n = int(input())
h = []
g = []
num = 0
for i in range(n):
m = map(int,input().split("\n"))
h.append(m)
for j in range(n):
ni = map(int,input().split("\n"))
h.append(ni)
當您使用input
讀取時,您將整個當前輸入行作為一個字符串,因此您在第一個之后對input
每個調用都將返回'1 2'
、 '2 4'
,最后是'3 4'
。
您需要將這些字符串拆分
(空格),然后將值轉換為整數並將它們附加到h
和g
列表中。 例如:
for i in range(n):
this_h, this_g = map(int, input().split(' '))
h.append(this_h)
g.append(this_g)
print(h)
print(g)
輸出:
[1, 2, 3]
[2, 4, 4]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.