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 Mã ví dụ đơn giản Output::int[]
cho mỗi phần.n = 2 # how many numbers to accept
numbers = [int[num] for num in input[].split[" ", n-1]]
print[numbers]
Đ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.
Vui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;
Có nhiều phương pháp khác nhau để làm điều này. Người dùng có thể nhập nhiều đầu vào trong một dòng được phân tách bằng một khoảng trắng. Giả sử tôi muốn người dùng nhập 5 số dưới dạng
1 2 3 4 5
mylist=list[map[int,input["Enter 5 numbers: "].split[]]
Điều này sẽ được nhập trong một dòng duy nhất. Nhưng hãy nhớ rằng bất kỳ đầu vào nào được coi là một chuỗi trong Python. Vì vậy, bạn sẽ phải chuyển đổi các giá trị này thành các giá trị số nguyên. Ngoài ra, bạn sẽ cần truy cập các giá trị đã nhập riêng này. Đối với điều đó, bạn có thể chuyển đổi các giá trị thành số nguyên và thêm chúng vào danh sách. Trong Python, thật kỳ lạ, không có mảng, mà là danh sách. Vì vậy, mã của bạn sẽ trông giống như thế này:
Bạn có thể muốn xem xét việc thực hiện các chức năng split[]
và map[]
. Bạn có thể tìm thấy chúng trong tài liệu chính thức. Nhưng đây là một lời giải thích nhỏ: hàm
lst = list[map[int, input[].split[]]]
print[lst]
0 sẽ chuyển đổi từng đầu vào một thành hàm được cung cấp, int trong trường hợp này. Hàm này tương đương với một vòng lặp cho lặp lại từng phần tử và áp dụng một số hàm biến đổi. Hàm split[]
sẽ phân chia đầu vào theo không gian trắng nếu không có dấu tách nào được cung cấp trong ngoặc. Theo mặc định, hàm map [] trả về một đối tượng bản đồ. Vì vậy, để chuyển đổi nó thành một danh sách, chúng tôi sử dụng lst = list[map[int, input[].split[]]]
print[lst]
2. Cải thiện bài viết
Lưu bài viết
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Chẳng hạn, trong c, chúng ta có thể làm một cái gì đó như thế này:
Một giải pháp là sử dụng raw_input [] hai lần.
Một giải pháp khác là sử dụng Split []
x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]
Lưu ý rằng chúng tôi không phải chỉ định rõ ràng sự phân chia [‘] vì split [] sử dụng bất kỳ ký tự khoảng trắng nào làm dấu phân cách mặc định.
Một điều cần lưu ý trong mã Python trên là, cả X và Y sẽ là chuỗi. Chúng ta có thể chuyển đổi chúng sang int bằng cách sử dụng một dòng khác
Dưới đây là một mã dòng hoàn chỉnh để đọc hai biến số nguyên từ đầu vào tiêu chuẩn bằng cách sử dụng phân chia và danh sách hiểu biết
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]
8 lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
0Abhishek Shukla. Please write comments if you find anything incorrect, or you want to share more
information about the topic discussed aboveVui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;
Cải thiện bài viết
Vui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;
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;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 phương thức Split []
input[].split[separator, maxsplit]
Sử dụng danh sách hiểu
Python3
Sử 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.
score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
9mylist=list[map[int,input["Enter 5 numbers: "].split[]]
0score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6mylist=list[map[int,input["Enter 5 numbers: "].split[]]
3mylist=list[map[int,input["Enter 5 numbers: "].split[]]
4score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7mylist=list[map[int,input["Enter 5 numbers: "].split[]]
6Cú pháp: & nbsp;
score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]5
mylist=list[map[int,input["Enter 5 numbers: "].split[]]
0score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6x, y = [int[x], int[y]] # We can also use list comprehension x, y = [int[x] for x in [x, y]]9
mylist=list[map[int,input["Enter 5 numbers: "].split[]]
4score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6input[].split[separator, maxsplit]3
input[].split[separator, maxsplit]4
score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7mylist=list[map[int,input["Enter 5 numbers: "].split[]]
6input[].split[separator, maxsplit]7
lst = list[map[int, input[].split[]]]
print[lst]
4 x, y = input["Enter a two value: "].split[]
print[x, y]
1x, y = input["Enter a two value: "].split[]
print[x, y]
6score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
5score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
6score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6int[]
5int[]
6int[]
7int[]
8score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7mylist=list[map[int,input["Enter 5 numbers: "].split[]]
6lst = list[map[int, input[].split[]]]
print[lst]
9lst = list[map[int, input[].split[]]]
print[lst]
4 split[]
3x, y = input["Enter a two value: "].split[]
print[x, y]
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]
6x, y = input["Enter a two value: "].split[]
print[x, y]
8x, y = input["Enter a two value: "].split[]
print[x, y]
1x, y = input["Enter a two value: "].split[]
print[x, y]
6map[]
1score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6map[]
5mylist=list[map[int,input["Enter 5 numbers: "].split[]]
0Output:
Sử 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;List comprehension :
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
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]
8 lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
0 x, y = input["Enter a two value: "].split[]
print[x, y]
1__score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
11mylist=list[map[int,input["Enter 5 numbers: "].split[]]
0score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
15mylist=list[map[int,input["Enter 5 numbers: "].split[]]
4score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7mylist=list[map[int,input["Enter 5 numbers: "].split[]]
6mylist=list[map[int,input["Enter 5 numbers: "].split[]]
7lst = 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]
8 lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
0score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
11mylist=list[map[int,input["Enter 5 numbers: "].split[]]
0score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
15mylist=list[map[int,input["Enter 5 numbers: "].split[]]
4score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
41input[].split[separator, maxsplit]4
score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7mylist=list[map[int,input["Enter 5 numbers: "].split[]]
6lst = 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]
8 lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
0 x, y = input["Enter a two value: "].split[]
print[x, y]
1__score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6int[]
5int[]
6int[]
7lst = list[map[int, input[].split[]]]
print[lst]
62score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7mylist=list[map[int,input["Enter 5 numbers: "].split[]]
6mylist=list[map[int,input["Enter 5 numbers: "].split[]]
7lst = 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]
8 lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
0score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
79lst = list[map[int, input[].split[]]]
print[lst]
80Đầu ra: & nbsp; & nbsp;
Lưu ý: Các ví dụ trên lấy đầu vào được phân tách bằng không gian.Trong trường hợp chúng tôi muốn lấy đầu vào được phân tách bằng dấu phẩy [,], chúng tôi có thể sử dụng các mục sau: & nbsp;The above examples take input separated by spaces. In case we wish to take input separated by comma [, ], we can use the following:
Python3
lst = list[map[int, input[].split[]]]
print[lst]
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]
6lst = list[map[int, input[].split[]]]
print[lst]
7lst = list[map[int, input[].split[]]]
print[lst]
8 lst = list[map[int, input[].split[]]]
print[lst]
9x, y = input["Enter a two value: "].split[]
print[x, y]
0 x, y = input["Enter a two value: "].split[]
print[x, y]
1__score, name = int[input['Enter Score: ']], input['Enter name:']
print[score]
print[name]
7x, y = input["Enter a two value: "].split[]
print[x, y]
6lst = list[map[int, input[].split[]]]
print[lst]
79lst = list[map[int, input[].split[]]]
print[lst]
80Vui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;