如何获取用户输入并将其放入 python 数组中的新数组中?

How to take user input and put it in a new array within an array in python?

我正在尝试将用户输入(只是一个整数列表)放入一个已经存在且其中包含一个元素的列表中。我不确定是否可以从现有列表中的一个元素中创建一个列表 运行。最终可能会向现有列表中添加更多元素。

代码如下:

days = ["Monday"]

days[0] = [int(x) for x in input("Please enter your schedule: ").split()]

print(days)

我希望结果能给我一个列表中的列表,但实际结果是:

days[0] = [int(x) for x in input("Please enter your schedule: ").split()]
ValueError: invalid literal for int() with base 10: '1000,'

你可以这样做:

days = ["Monday"]    
days.append( [int(x) for x in input("Please enter your schedule: ").split()] )
print(days)

如果您在命令提示符下提供 1000 2000 3000,那将给您 ["Monday", [1000, 2000, 3000]]

如果你这样做:

days = ["Monday"]

input_data = input("Please enter your schedule: ")
split_data = input_data.split()
for item in split_data:
    days.append(item)
print(days)

您将获得["Monday", 1000, 2000, 3000]

或者你可以像这样使用字典:

days = {}
days["Monday"] = [int(x) for x in input("Please enter your schedule: ").split()]
print(days)

获得{'Monday': [1000, 2000, 3000]}