Hướng dẫn how do you take n inputs in one line in for loop in python? - làm thế nào để bạn lấy n đầu vào trong một dòng trong vòng lặp for trong python?

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

lst = list[map[int, input[].split[]]]

print[lst]
2 cho mỗi phần.

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

  • Đọc
  • Bàn luận
  • 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]
    1

    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. 

    Python3

    Cú pháp: & nbsp;

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    1
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    2

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    5
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    6

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    8

    Ví dụ: & nbsp;

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    3
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    4
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    5
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    7
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    8

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    num = []
    x = int[input[]]
    for i in range[1, x+1]:
        num.append[input[]]
    
    1
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    6

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    num = []
    x = int[input[]]
    for i in range[1, x+1]:
        num.append[input[]]
    
    5
    num = []
    x = int[input[]]
    for i in range[1, x+1]:
        num.append[input[]]
    
    6

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    8

    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    4
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    5
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    score, name = int[input['Enter Score: ']], input['Enter name:']
    
    print[score]
    print[name]
    3
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    8

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    2
    145
    1278
    
    7
    2
    145
    1278
    
    8
    2
    145
    1278
    
    9
    2
    145 1278
    
    0

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    8

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    score, name = int[input['Enter Score: ']], input['Enter name:']
    
    print[score]
    print[name]
    7
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    2

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    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']
    
    7
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    2

    Output:   
     

    num = []
    x = int[input[]]
    for i in range[1, x+1]:
        num.append[input[]]
    
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    4
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    5
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    7
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    8
    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

    2
    145 1278
    
    3
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    4
    2
    145 1278
    
    5
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    2
    145 1278
    
    7
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    2
    145 1278
    
    9
    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']
    
    0
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    5__16

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    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]
    
    3
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    2

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    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]
    
    7
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    6

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    8

    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;

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    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]
    
    3
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    2

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    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]
    
    7
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    6

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    23
    num = []
    x = int[input[]]
    for i in range[1, x+1]:
        num.append[input[]]
    
    6

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    8

    2
    145 1278
    
    3
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    4
    2
    145 1278
    
    5
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    2
    145 1278
    
    7
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    2
    145 1278
    
    9
    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']
    
    0
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    5__16

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    2
    145
    1278
    
    7
    2
    145
    1278
    
    8
    2
    145
    1278
    
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    44

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    8

    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;

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    61
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    62

    lst = list[map[int, input[].split[]]]
    
    print[lst]
    3
    lst = 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]
    
    1
    2
    145 1278
    
    9
    x = int[input[]]
    y = input[]
    numbers_list = y.split[" "][:x]
    array = list[map[int, numbers_list]]
    print[max[array]-min[array]-x+1]
    
    3
    x = 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__

     

    x, y = input["Enter a two value: "].split[]
    
    print[x, y]
    
    9
    lst = 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]
    
    1
    2
    145 1278
    
    9
    x = int[input[]]
    y = input[]
    numbers_list = y.split[" "][:x]
    array = list[map[int, numbers_list]]
    print[max[array]-min[array]-x+1]
    
    3
    x = 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__
    The above examples take input separated by spaces. In case we wish to take input separated by comma [, ], we can use the following: 

    Python3

    2
    145 1278
    
    3
    lst = 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]
    
    1
    2
    145 1278
    
    9
    x = int[input[]]
    y = input[]
    numbers_list = y.split[" "][:x]
    array = list[map[int, numbers_list]]
    print[max[array]-min[array]-x+1]
    
    3
    x = 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]
    9
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    6
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    61
    lst = list[map[int, input[].split[]]]
    
    print[lst]
    62

    Vui lòng xem //ide.geeksforgeek.org/bhf0cxr4mx để chạy mẫu. & Nbsp;
     


    Làm thế nào để bạn lấy một danh sách các đầu vào trong một dòng trong Python?

    Nhập một danh sách bằng hàm input [] và phạm vi []..
    Đầu tiên, tạo một danh sách trống ..
    Tiếp theo, chấp nhận kích thước danh sách từ người dùng [nghĩa là, số lượng các phần tử trong danh sách].
    Chạy vòng lặp cho đến kích thước của danh sách bằng cách sử dụng hàm cho vòng lặp và phạm vi [] ..
    Sử dụng hàm input [] để nhận số từ người dùng ..

    Làm thế nào để bạn lấy nhiều đầu vào từ một vòng lặp trong Python?

    Để lấy nhiều đầu vào bằng cách sử dụng một vòng lặp cho vòng lặp: khai báo một biến mới và khởi tạo nó vào một danh sách trống.Sử dụng lớp [] lớp để lặp n lần trong một vòng lặp.Trên mỗi lần lặp, nối đầu vào người dùng vào danh sách.Declare a new variable and initialize it to an empty list. Use the range[] class to loop N times in a for loop. On each iteration, append the user input to the list.

    Làm thế nào để bạn lấy nhiều đầu vào số nguyên trong một dòng trong Python?

    Cú pháp:..
    Cú pháp:.
    Input []. Chia [phân tách, MaxSplit] Ví dụ:.
    # Lấy nhiều đầu vào cùng một lúc.# và loại đúc bằng hàm danh sách [].x = list [map [int, input ["Nhập nhiều giá trị:"] .split []]] ....
    # Lấy nhiều đầu vào cùng một lúc.x = [int [x] cho x trong đầu vào ["Nhập nhiều giá trị:"] .split []].

    Làm thế nào để bạn viết một vòng lặp trong python trên một dòng?

    Làm thế nào để viết một vòng lặp cho một dòng mã Python duy nhất ?..
    Phương pháp 1: Nếu cơ thể vòng lặp bao gồm một câu lệnh, chỉ cần viết câu lệnh này vào cùng một dòng: Đối với I trong phạm vi [10]: in [i].....
    Phương pháp 2: Nếu mục đích của vòng lặp là tạo danh sách, hãy sử dụng danh sách hiểu thay vào đó: bình phương = [i ** 2 cho i trong phạm vi [10]] ..

    Chủ Đề