【问题标题】:Python - How to check the place value in a listPython - 如何检查列表中的位置值
【发布时间】:2013-04-05 23:27:46
【问题描述】:

Python:列表检查

我正在尝试编写一个 Python (2.7.3) 程序,该程序将检查列表以查看是否已给出列表的放置值?

input = raw_input("(Enter three numbers separated by commas) >: ")
input_list = input.split(" ")
replacing_letters = [str(x.strip(" ")) for x in input_list]
'if replacing_letters[2] is blank:'
    print"Incomplete"
'elif replacing_letters[2] is full':
    print replacing_letters

因此,如果我要在输入中输入三个数字,它会将列表打印给用户。但是假设我只输入两个数字。它将返回给用户“不完整”。

对实现此目的的方法或代码有什么建议吗?

【问题讨论】:

  • 提示:检查列表中有多少项目
  • 抛开问题,你不想用逗号分割吗?

标签: python arrays list input


【解决方案1】:

您可以使用the len() built-in function 获取列表的长度。然后,您可以对此进行简单检查:

if len(input_list) <= 2:
    ...
else:
    ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-09
    • 2016-05-10
    • 2017-01-02
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多