作者busystudent (busystudent)
看板Python
標題[問題] 關於list的append問題
時間Sat Apr 9 17:31:08 2016
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
推 aweimeow: namelist = namelist + shop_link 04/09 17:41
→ aweimeow: 因為你的 shop_link 也是 list 所以不能這樣 append 04/09 17:43
→ busystudent: 謝謝 我想我對append真的不熟悉 04/09 17:54
→ freeunixer: 不是有 extend 可以用.............................. 04/09 19:19
推 aweimeow: 樓上大大學習了 <(_ _)> 我不知道有這東西 04/09 20:42
→ freeunixer: .................................................. 04/09 21:10
→ freeunixer: 還是找本書把 type.func 看一次,或至少 help(type) 吧 04/09 21:11
推 largesperm: 用英文去 google 再破也可以 python list plus 04/09 21:17
→ largesperm: 通常 stackvoerflow 都會有東西 04/09 21:17
→ largesperm: stackoverflow 很多怪問題都有解答 04/09 21:18
→ busystudent: 謝謝大家的回覆 04/09 21:33
→ MOONY135: 你要去搞懂list在做啥 append是以加一個元素進來 04/10 08:31
→ MOONY135: 當然一個LIST本身也可以當成一個元素了 04/10 08:32