看板 Python 關於我們 聯絡資訊
大家好, 小弟是初學者, 最近參照網路上的範例程式遇到一個覺得還奇怪的寫法, ------------------------------------------ import urllib.request from bs4 import BeautifulSoup response = urllib.request.urlopen('http://php.net/') html = response.read() print(html) soup = BeautifulSoup(html, "html5lib") text = soup.get_text(strip=True) print(type(text.split())) tokens = [t for t in text.split()] print(type(tokens)) ------------------------------------------ 疑惑的是 text.split()已經是list, 可以直接指派給tokens使用, 不知為何還要將text.splt()for loop之後在放到t這個list呢? 謝謝 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.163.110.208 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1542679225.A.A35.html
ripple0129: 看起來是多此一舉 11/20 10:12
TuCH: 是的 這行程式碼是多餘的 11/20 10:13
comodore: 謝謝大家 11/20 10:30
TitanEric: 這範例真棒… 11/20 21:59