【问题标题】:How to turn users input into a list in Python?如何将用户输入转换为 Python 中的列表?
【发布时间】:2014-06-07 03:55:43
【问题描述】:

到目前为止,这是我的代码。

def pfunc():
    portList = []
    user_choice_port = userPort.get()
    portList.append(user_choice_port)

userPort 基本上是一个文本变量,用于用户在输入框(将是端口)中输入的内容。用户将像这样输入端口号:

23, 80, 44, etc.

我如何获取这些数字并将它们放入列表中。因为每当我这样做时,它都会将它们作为一个字符串放入,如下所示:

['23, 80, 44']

当我希望它看起来像这样时:

[23,80,44]

我无法弄清楚,所以任何帮助将不胜感激

【问题讨论】:

    标签: list python-2.7 tkinter


    【解决方案1】:

    这应该会有所帮助。

    user_choice_port = "23, 80, 44"
    print map(int, user_choice_port.split(","))
    print [int(n) for n in user_choice_port.split(",")]
    

    【讨论】:

    • 感谢这工作!但是我怎样才能 .append() 将这些结果添加到列表中?就像我有一个名为 portList = [] 的列表。我怎么能接受输入,然后直接将它们 .append() 输入其中?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    相关资源
    最近更新 更多