如何在循环内的 Python 中的同一行中获取输入?

How to take inputs in same line in Python which is inside a loop?

如何给号。元素和列表在同一行?以下是示例:

1
8
234 567 321 345 123 110 767 111 

这里我想询问用户 he/she 想要多少个元素 N 并且在指定我希望用户在 he/she 输入 N 的同一行中输入元素之后.

T = int(input())
for x in range(T):
    N = int(input())
    nums = list(map(int, input().split()))

使用这段代码,我可以在不同的行中给出 N 和 nums,但我希望它们在同一行中,然后评估解决方案。 这是完整的代码:

def largest(num):
    num_str = str(num)
    i = 9
    while i >= 0:
        if str(i) in num_str:
            return i
        i -= 1


def smallest(num):
    num_str = str(num)
    i = 0
    while i <= 9:
        if str(i) in num_str:
            return i
        i += 1


def pairs(num):
    if num == 2:
        return 1
    if num == 3:
        return 2
    return 0


T = int(input())
for x in range(T):
    N = int(input())
    nums = list(map(int, input().split()))

    assert(len(nums) == N)

    bitscore = [""]*N

    for i in range(len(bitscore)):
        curr_score = str(11 * largest(nums[i]) + 7 * smallest(nums[i]))
        if len(curr_score) > 2:
            curr_score = curr_score[-2:]
        bitscore[i] = curr_score

    odd_pos_freq = [0] * 10
    even_pos_freq = [0] * 10

    for i in range(len(bitscore)):
        index = int(bitscore[i][0])
        if (i + 1) % 2 == 0:
            even_pos_freq[index] += 1
        else:
            odd_pos_freq[index] += 1

    count_pairs = [0] * 10

    for i in range(10):
        if even_pos_freq[i] <= 1 and odd_pos_freq[i] <= 1:
            continue
        count_pairs[i] += pairs(even_pos_freq[i]) + pairs(odd_pos_freq[i])
        count_pairs[i] = min(2, count_pairs[i])

    print(sum(count_pairs))

我知道这个问题听起来很愚蠢,但很多时候我都被这些小事卡住了,即使逻辑正确我也无法提交正确答案。

替换您的代码:

N = int(input())
nums = list(map(int, input().split()))

与:

line = input('')
nums = list(map(int, line.split()))
N = nums[0]
nums = nums[1:]