code.club

標題: python中list()的一些注意事項 [打印本頁]

作者: jagabee    時間: 2017-6-5 23:31
標題: python中list()的一些注意事項
本帖最後由 jagabee 於 2017-6-5 23:33 編輯

直接將變數指定一個list,x = [1,"hello",333,"day"]
若要使list()函數來指定,x = list([]),參數一定要直接是有[]的list格式,例如 x = list([123,"hello",999,"ok"]),唯二的例外是如果直接在list()中給一個字串,則視為給一個字元的list。例如
x = list("idea"),則 x = ['i','d','e','a'] 。另外是給一個range則視為各數字的list,例如 x = list(range(2,9)),則 x = [2,3,4,5,6,7,8]




歡迎光臨 code.club (http://code.club/) Powered by Discuz! X3.2