2014-10-10 123 views
0

我對python相當陌生。 我試圖將原始輸入存儲到一個空列表。顯然,輸入沒有進入空白列表。那麼出了什麼問題?[]追加後變爲無

Latitude = [] 
Longitude = [] 
print Latitude #**THIS GIVES []** 

Lat_input = raw_input("What is your latitude:") 
Latitude = Latitude.append(Lat_input) 
print Latitude # **HERE I GOT NONE** 

Long_input = raw_input("WHat is your longitude:") 
Longitude = Longitude.append(Long_input) 

我擡頭看了一些其他貼子,仍然沒有弄清楚我做錯了什麼。 我錯過了什麼?爲什麼我的名單沒有了? 謝謝,夥計們!

+0

可能重複給出None作爲結果](http://stackoverflow.com/questions/26151795/list-append-gives-none-as-result) – 2014-10-11 00:13:00

回答

3

append是就地操作;它不會返回一個值。

只要運行:

Longitude.append(Long_input) 

...不是

Longitude = Longitude.append(Long_input) 

這是由設計和意圖:返回None,而不是一個值清楚地表明,一個函數被調用因爲它的副作用而不是它的回報價值。

如果您沒有要修改的地方現有Longitude,而是想創造與追加新項目一個新的列表,那麼你可以改用:

Longitude = Longitude + [Long_input] 
[清單追加的
+0

非常感謝您的明確! – WHZW 2014-10-12 00:35:06