2013-06-19 19 views
3

我有一個數組(列表),我想檢查它的大小是否等於1,如果它是,那麼它需要添加一個新的如圖所示。Python 2.7,我如何獲取/檢查列表的大小?

## 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

任何想法?

+1

你甚至谷歌? http://stackoverflow.com/questions/1712227/get-the-size-of-a-list-in-python – mrKelley

+0

https://www.google.co.in/search?q=how+to+get+在+ python中輸入+ a + list +的長度,在google上鍵入一個簡單的查詢就可以在幾秒鐘內解決你的問題。 –

+0

甚至不需要谷歌:http://stackoverflow.com/search?q=get+list+size+python – msw

回答

9

使用就可以了len() function

if len(appended_data) == 1: 

簡短演示:

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