Bạn có thể sử dụng danh sách hiểu để lấy N đầu vào trong một dòng trong Python. Chuỗi đầu vào được chia thành các phần N, sau đó danh sách comp tạo một danh sách mới bằng cách áp dụng
2 cho mỗi phần.lst = list[map[int, input[].split[]]]
print[lst]
Mã ví dụ đơn giản
n = 2 # how many numbers to accept
numbers = [int[num] for num in input[].split[" ", n-1]]
print[numbers]
Output::
Đoạn trích sau đây sẽ ánh xạ đầu vào dòng đơn được phân tách bằng không gian trắng vào danh sách các số nguyên
lst = list[map[int, input[].split[]]]
print[lst]
Output::
1 2 3 [1, 2, 3]
[1, 2, 3]
Làm thế nào để lấy nhiều đầu vào của các loại dữ liệu khác nhau trong một dòng trong Python?
Trả lời: Ví dụ lấy 2 giá trị đầu vào.: Example take 2 input values.
x, y = input["Enter a two value: "].split[]
print[x, y]
Output::
Nhập hai giá trị: 1 x1 x
1 X
HOẶC
score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
Hãy bình luận nếu bạn có bất kỳ nghi ngờ và đề xuất nào về chủ đề đầu vào Python này.
Lưu ý: IDE: & NBSP; Pycharm & NBSP; 2021.3.3 [Phiên bản cộng đồng] IDE: PyCharm 2021.3.3 [Community Edition]
Windows 10
Python 3.10.1
Tất cả & nbsp; ví dụ python & nbsp; là trong & nbsp; Python & nbsp; 3, vì vậy có thể khác với các phiên bản Python 2 hoặc nâng cấp. Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.
Bằng cấp về Khoa học máy tính và Kỹ sư: Nhà phát triển ứng dụng và có nhiều ngôn ngữ lập trình kinh nghiệm. Sự nhiệt tình cho công nghệ và thích học kỹ thuật.
Tôi không biết làm thế nào để có được đầu vào tùy thuộc vào sự lựa chọn của người dùng. I E. 'Bạn muốn nhập bao nhiêu số?' Nếu câu trả lời 5, thì mảng của tôi có 5 khoảng trống cho 5 số nguyên trong một dòng cách nhau không gian.
num = []
x = int[input[]]
for i in range[1, x+1]:
num.append[input[]]
Mã trên hoạt động, tuy nhiên các đầu vào được phân chia bởi enter [dòng tiếp theo]. I E.:
2
145
1278
Tôi muốn có được:
2
145 1278
Tôi sẽ biết ơn một số trợ giúp.
EDIT:
x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
Điều này có vẻ hoạt động. Nhưng tại sao tôi nhận được lỗi "giới hạn bộ nhớ vượt quá"?
Chỉnh sửa: Bất cứ phương pháp nào tôi sử dụng, tôi có cùng một vấn đề.
x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
hoặc
x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
array = list[map[int, num]]
print[max[array]-min[array]-x+1]
hoặc
lst = list[map[int, input[].split[]]]
print[lst]
0 Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
- Bàn luận
- Nhà phát triển thường muốn người dùng nhập nhiều giá trị hoặc đầu vào trong một dòng. Trong C ++/C, người dùng có thể lấy nhiều đầu vào trong một dòng bằng cách sử dụng Scanf nhưng trong Python, người dùng có thể lấy nhiều giá trị hoặc đầu vào trong một dòng bằng hai phương thức. & NBSP;
Sử dụng phương thức Split []split[] method :
This function helps in getting multiple inputs from users. It breaks the given input by the specified separator. If a separator is not provided then any white space is a
separator. Generally, users use a split[] method to split a Python string but one can use it in taking multiple inputs.
Sử dụng danh sách hiểu
lst = list[map[int, input[].split[]]]
print[lst]
1Sử dụng phương thức Split []: & NBSP; Hàm này giúp nhận được nhiều đầu vào từ người dùng. Nó phá vỡ đầu vào đã cho bởi bộ phân cách được chỉ định. Nếu một dải phân cách không được cung cấp thì bất kỳ không gian trắng nào là một dấu tách. Nói chung, người dùng sử dụng phương thức chia [] để phân chia chuỗi Python nhưng người ta có thể sử dụng nó để thực hiện nhiều đầu vào.
Python3
Cú pháp: & nbsp;
lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x, y = input["Enter a two value: "].split[]
print[x, y]
1x, y = input["Enter a two value: "].split[]
print[x, y]
2lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x, y = input["Enter a two value: "].split[]
print[x, y]
5x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
8Ví dụ: & nbsp;
lst = list[map[int, input[].split[]]]
print[lst]
3lst = list[map[int, input[].split[]]]
print[lst]
4 lst = list[map[int, input[].split[]]]
print[lst]
5lst = list[map[int, input[].split[]]]
print[lst]
6lst = list[map[int, input[].split[]]]
print[lst]
7lst = list[map[int, input[].split[]]]
print[lst]
8lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6num = []
x = int[input[]]
for i in range[1, x+1]:
num.append[input[]]
1x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6num = []
x = int[input[]]
for i in range[1, x+1]:
num.append[input[]]
5num = []
x = int[input[]]
for i in range[1, x+1]:
num.append[input[]]
6lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
8x, y = input["Enter a two value: "].split[]
print[x, y]
9lst = list[map[int, input[].split[]]]
print[lst]
4 lst = list[map[int, input[].split[]]]
print[lst]
5lst = list[map[int, input[].split[]]]
print[lst]
6score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
3lst = list[map[int, input[].split[]]]
print[lst]
8lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
62
145
1278
72
145
1278
82
145
1278
92
145 1278
0lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
8lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
2lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
7x, y = input["Enter a two value: "].split[]
print[x, y]
2Output:
9num = []
x = int[input[]]
for i in range[1, x+1]:
num.append[input[]]
4 lst = list[map[int, input[].split[]]]
print[lst]
5lst = list[map[int, input[].split[]]]
print[lst]
6lst = list[map[int, input[].split[]]]
print[lst]
7lst = list[map[int, input[].split[]]]
print[lst]
8List comprehension : lst = list[map[int, input[].split[]]]
print[lst]
List comprehension is an elegant way to define and create list in Python. We can create lists just like mathematical statements in one line only. It is also used in getting multiple inputs from a user.
Example:
Python3
2
145 1278
3lst = list[map[int, input[].split[]]]
print[lst]
4 2
145 1278
5lst = list[map[int, input[].split[]]]
print[lst]
62
145 1278
7lst = list[map[int, input[].split[]]]
print[lst]
62
145 1278
9x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
0lst = list[map[int, input[].split[]]]
print[lst]
5__16lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
array = list[map[int, num]]
print[max[array]-min[array]-x+1]
3x, y = input["Enter a two value: "].split[]
print[x, y]
2lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
array = list[map[int, num]]
print[max[array]-min[array]-x+1]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
8Sử dụng danh sách hiểu biết: & NBSP; Danh sách hiểu là một cách thanh lịch để xác định và tạo danh sách trong Python. Chúng ta có thể tạo danh sách giống như các câu lệnh toán học chỉ trong một dòng. Nó cũng được sử dụng để nhận nhiều đầu vào từ người dùng. & NBSP;
lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
array = list[map[int, num]]
print[max[array]-min[array]-x+1]
3x, y = input["Enter a two value: "].split[]
print[x, y]
2lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
array = list[map[int, num]]
print[max[array]-min[array]-x+1]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6lst = list[map[int, input[].split[]]]
print[lst]
23num = []
x = int[input[]]
for i in range[1, x+1]:
num.append[input[]]
6lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
82
145 1278
3lst = list[map[int, input[].split[]]]
print[lst]
4 2
145 1278
5lst = list[map[int, input[].split[]]]
print[lst]
62
145 1278
7lst = list[map[int, input[].split[]]]
print[lst]
62
145 1278
9x = int[input[]]
while True:
attempt = input[]
try:
num = [int[val] for val in attempt.split[" "]]
if len[num]== x:
break
else:
print['Error']
except:
print['Error']
0lst = list[map[int, input[].split[]]]
print[lst]
5__16lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
62
145
1278
72
145
1278
82
145
1278
9lst = list[map[int, input[].split[]]]
print[lst]
44lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
8Sử dụng danh sách hiểu biết: & NBSP; Danh sách hiểu là một cách thanh lịch để xác định và tạo danh sách trong Python. Chúng ta có thể tạo danh sách giống như các câu lệnh toán học chỉ trong một dòng. Nó cũng được sử dụng để nhận nhiều đầu vào từ người dùng. & NBSP;
lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6lst = list[map[int, input[].split[]]]
print[lst]
61lst = list[map[int, input[].split[]]]
print[lst]
62
3lst = list[map[int, input[].split[]]]
print[lst]
4 lst = list[map[int, input[].split[]]]
print[lst]
1x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
92
145 1278
3x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
4 x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
3__2
145 1278
9x, y = input["Enter a two value: "].split[]
print[x, y]
4 lst = list[map[int, input[].split[]]]
print[lst]
1x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
92
145 1278
3x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
4 x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
3__The above examples take input
separated by spaces. In case we wish to take input separated by comma [, ], we can use the following: 2
145 1278
Python3
2
145 1278
3lst = list[map[int, input[].split[]]]
print[lst]
4 x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
12
145 1278
9x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
3x = int[input[]]
y = input[]
numbers_list = y.split[" "][:x]
array = list[map[int, numbers_list]]
print[max[array]-min[array]-x+1]
4 2
145 1278
3__lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
6lst = list[map[int, input[].split[]]]
print[lst]
61lst = list[map[int, input[].split[]]]
print[lst]
62Vui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;