【问题标题】:Is there a way to make Python List by Input Prompt?有没有办法通过输入提示制作 Python 列表?
【发布时间】:2012-01-04 23:57:04
【问题描述】:

我正在创建一个 Python 邮件列表,但我在函数结束时遇到了问题。

问题是,列表必须是这样的:

['first@google.com', 'second@google.com', 'third@google.com']

我当前的代码:

mailinputs = raw_input('Enter all mails with comma: ')
receivers = [mailinputs]

如果你输入:

'first@google.com', 'second@google.com', 'third@google.com'

出现这样的错误:

Probe failed: Illegal envelope To: address (invalid domain name):

否则,如果您键入:

first@google.com, second@google.com, third@google.com

只有 first@google.com 会收到邮件。

我该怎么办?

【问题讨论】:

  • 您的意思是列表,而不是字典。

标签: python dictionary input


【解决方案1】:

raw_input() 的返回是一个字符串。你需要用逗号分割它,然后你会得到一个列表:

>>> 'first@google.com,second@google.com,third@google.com'.split(',')
['first@google.com', 'second@google.com', 'third@google.com']

所以在你的例子中:

mailinputs = raw_input('Enter all mails with comma: ')
receivers = mailinputs.split(',')

可以执行另一个步骤来删除每封电子邮件之前/之后的所有空格:

mailinputs = raw_input('Enter all mails with comma: ')
receivers = [x.strip() for x in mailinputs.split(',')]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-08
    • 1970-01-01
    • 1970-01-01
    • 2021-03-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多