如何将str列表转换为int列表?

时间:2020-01-13 15:11:14

标签: python list recursion

我想将给定的输入转换为整数列表。我不能使用:求和,映射,归约,缩小,过滤,导入,评估,排序,执行,编译和单个。 这是我写的:

def convert_int(my_line):
    i = len(my_line) - 1

    if i == 0:
        return int(my_line[0])
    else:
        return [int(my_line[i]), convert_int(my_line[:i])]

line = input().split()

print(convert_int(line))

使用输入:

1 2 3 4 5

输出为:

[5, [4, [3, [2, 1]]]]

该怎么做?

2 个答案:

答案 0 :(得分:1)

您需要连接两个列表,而不是将递归结果嵌入到列表中。

<div style="position:absolute">
  <div class="apr-laflex-container apr-and-text-ellipsis-container">
    <div>
      <span>Status<span>
			</div>
		</div>	
	    <div class="apr-laflex-container apr-and-text-ellipsis-container">
			<div>
				<span>StartDateFrom</span>
    </div>
    <div>
      <span>StartDateTo </span>
    </div>
    <div>
      <div class="ScheduledOperation">
        <input type="checkbox" name="Scheduled Operations" checked>
        <label for="Scheduled Operations">Scheduled Operations</label>
      </div>
      <div class="UnscheduledOperation">
        <input type="checkbox" name="Unscheduled Operations">
        <label for="Unscheduled Operations">Unscheduled Operations</label>
      </div>
    </div>
  </div>

答案 1 :(得分:0)

另一种解决方案(如问题中所述,不进行总和,映射,归约,过滤,导入,评估,排序,执行,编译和单一):

line = '1 2 3 4 5'

line = line.split()
i = iter(line)
line[:] = iter(lambda: int(next(i)), None)
print(line)

打印:

[1, 2, 3, 4, 5]