首页 > 解决方案 > 如何使用 list.split() 从用户获取确切数量的输入到列表

问题描述

输入说明:第一行包含数组的数量。随后的行包含数组的大小,后跟数组的元素。

输出描述:一个包含k个排序数组的排序元素的数组

Sample Input :
3
2
98 12
6
1 2 3 8 5 9
1
11
Sample Output :
12 98 1 2 3 5 8 9 11

我试过的代码:

n=int(input())
for i in range(n):
  k=int(input())
  for i in range(k):
    l.append(input().split())
 print(*sorted(l))

对于 2 个数字的第一个输入,我需要使用 input().split() 输入 2 个数字(98,12),但由于内部循环执行两次,我最终输入两次

标签: pythonlistsorting

解决方案


您在尝试的方面处于领先地位,但是当您尝试输入值以填充列表时,您混合了一些东西。
让我们使用以下骨架:

number_of_lists = int(input('Enter the number of lists : '))
for list_number in range(1, number_of_lists+1):
    current_list = []
    number_of_values = int(input('Enter the number of values in the list #%d : ' % list_number))
    # Here we enter the values of the current list,
    # and that's where things will differ a bit
    print('The list is : %s' % str(current_list))
    print('The sorted list is: %s' % str(sorted(current_list)))

根据输入列表值的方式,情况会有所不同:

  • 如果它们是一一输入的(即,您输入一个,然后按 Enter,然后按另一个,...)
  • 如果它们都在同一行中输入,用空格分隔,则不需要for循环,而是必须在空格上拆分输入以填充列表。

案例1:您一一输入值

在这种情况下,您可以放入列表中的值的数量由您之前输入的数字控制,该数字在for循环中使用。

number_of_lists = int(input('Enter the number of lists : '))
for list_number in range(1, number_of_lists+1):
    current_list = []
    number_of_values = int(input('Enter the number of values in the list #%d : ' % list_number))
    for value_number in range(number_of_values):
        value = int(input("Enter the value : "))
        current_list.append(value)
    print('The list is : %s' % str(current_list))
    print('The sorted list is: %s' % str(sorted(current_list)))

案例 2:您在同一行中输入所有值,所有值由

以防万一,因为您同时获得所有值,所以不需要for循环,但这需要您检查是否获得了正确数量的值。

多种策略:

  1. 您信任用户输入正确数量的值(我不建议这样做)
  2. 您要求用户输入,直到他输入足够的值
  3. 如果不够,则添加值,如果过多,则删除一些值

第一种可能性:您只需要求用户输入值:

all_values = input("Enter all the values : ").split()

第二种可能性:您必须使用一个while循环,在其中向用户询问值,直到您有足够的值:

all_values = []
while len(all_values) != number_of_values:
    all_values = input("Enter all the values : ").split()
    if len(all_values) != number_of_values:
        # Warning message for the user, not required but a good thing
        print("You didn't enter enough values, try again.")

第三种可能性:当用户输入的内容不够多时,您添加值,并在他给您更多时删除一些值:

all_values = input("Enter all the values : ").split()
if len(all_values) < number_of_values:
    # We don't have enough values... What do we do ?
    # Let's add 0's at the end, this will not change the final order
    while len(all_values) < number_of_values:
        all_values.append(0)
elif len(all_values) > number_of_values:
    # We have too much values... What do we do ?
    # Let's try to remove the last value until we have enough values
    while len(all_values) > number_of_values:
        all_values.pop()

一旦获得了正确数量的值,您仍然需要将每个项目设为整数:

current_list = [int(item) for item in all_values]

您终于可以对列表进行排序并打印出来了,

总结一下这种情况,使用我在顶部提供的骨架,并使用三种可能性之一来输入列表的值。

结论 那么使用哪一个呢?好吧,您可能已经猜到了我建议您使用第一种情况,它更易于使用和理解。

尽管如果您想使用第二种情况,您仍然需要选择如何控制您获得的值的数量:

  • 我强烈不鼓励您选择第一种可能性,在计算机科学中,相信用户提供他提供给您的数据从来都不是一个好的选择(不是您直接,而是您的程序,......)。
  • 第二个很有趣,因为它迫使用户输入确切数量的值,不多也不少。
  • 第三个在这一点上的限制较少,因为它可以处理比预期更多或更少的值,但这也可能是它的坏点。

希望这有帮助!


推荐阅读