【问题标题】:Python 2.7, How can I get/check the size of a list? [duplicate]Python 2.7,如何获取/检查列表的大小? [复制]
【发布时间】:2013-06-16 15:06:14
【问题描述】:

我有一个数组(列表),我想检查它的大小是否等于 1,如果是,则需要添加一个新的,如图所示。

## If appended data = 1 then append the new line:
if appended_data == 1:
    appeneded_data.append("") ## Add a new line if appended data has a size of 1

应该是一个相当简单的事情,但我无法解决:S

有什么想法吗?

【问题讨论】:

标签: python arrays list if-statement python-2.7


【解决方案1】:

使用len() function就可以了:

if len(appended_data) == 1:

简短演示:

>>> len([])
0
>>> len([1])
1
>>> len([1, 2])
2

【讨论】:

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