所以,我只是想解决hackerrank.com
上的问题这是我的代码:
if __name__ == '__main__':
N = int(input())
num_list = []
for numberOfCommands in range(N):
command , * numbers = input().split(" ")
numbers = [int(i) for i in numbers]
print(numbers)
if command == 'insert':
num_list.insert(numbers[0],numbers[1:])
elif command == 'print':
print(num_list)
else:
num_list.command(numbers[0])
问题是我打印list
时打印嵌套列表。我想要的是创建一个int
s。
答案 0 :(得分:0)
你可以flatten a list of lists这样:
flat_list = [element for sublist in num_list for element in sublist]
print(flat_list)
或使用reduce
功能:
import operator
from functools import reduce
reduce(operator.concat, num_list)