[問題] 關於list的append問題

看板Python作者 (busystudent)時間9年前 (2016/04/09 17:31), 9年前編輯推噓3(3010)
留言13則, 5人參與, 最新討論串1/1
hi 這幾天寫程式,有幾個關於list apeend的問題要跟大家請教。 1. 我寫了一個for迴圈,可以print 很多list,例如以下程式碼 for shop_link in shop_table: . .... print shop_link output: ['leerentveld', 'mohell', 'vinoph', ] ['mdefelice', 'iamthemomo', 'tvenchus', ] 我該如和變成以下的name_list呢?這種把兩種list內的值合起來 name_ist = ['leerentveld', 'mohell', 'vinoph', 'mdefelice', 'iamthemomo', 'tvenchus',] name_ist = ['leerentveld', 'mohell', 'vinoph', 'mdefelice', 'iamthemomo', 'tvenchus',] 我第一種嘗試為 name_list= [] for shop_link in shop_table: . .... print shop_link name_list.append(shop_link) output: [ ['leerentveld', 'mohell', 'vinoph', ], ['mdefelice', 'iamthemomo', 'tvenchus', ] ] 我打算使用append的方式一個個加入,可是輸出卻不是我想要的,變成[[ ],[ ]]了請問我該如何修改 -- Sent from my Windows -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.172.97.181 ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 1.172.97.181 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1460194271.A.728.html ※ 編輯: busystudent (223.140.224.116), 04/09/2016 17:37:29

04/09 17:41, , 1F
namelist = namelist + shop_link
04/09 17:41, 1F

04/09 17:43, , 2F
因為你的 shop_link 也是 list 所以不能這樣 append
04/09 17:43, 2F

04/09 17:54, , 3F
謝謝 我想我對append真的不熟悉
04/09 17:54, 3F

04/09 19:19, , 4F
不是有 extend 可以用..............................
04/09 19:19, 4F

04/09 20:42, , 5F
樓上大大學習了 <(_ _)> 我不知道有這東西
04/09 20:42, 5F

04/09 21:10, , 6F
..................................................
04/09 21:10, 6F

04/09 21:11, , 7F
還是找本書把 type.func 看一次,或至少 help(type) 吧
04/09 21:11, 7F

04/09 21:17, , 8F
用英文去 google 再破也可以 python list plus
04/09 21:17, 8F

04/09 21:17, , 9F
通常 stackvoerflow 都會有東西
04/09 21:17, 9F

04/09 21:18, , 10F
stackoverflow 很多怪問題都有解答
04/09 21:18, 10F

04/09 21:33, , 11F
謝謝大家的回覆
04/09 21:33, 11F

04/10 08:31, , 12F
你要去搞懂list在做啥 append是以加一個元素進來
04/10 08:31, 12F

04/10 08:32, , 13F
當然一個LIST本身也可以當成一個元素了
04/10 08:32, 13F
文章代碼(AID): #1N2ClVSe (Python)
文章代碼(AID): #1N2ClVSe (Python)