作者cuteSquirrel (可爱的小松鼠)
看板Python
标题Re: [问题] 优化程式码,转成 dict
时间Thu Dec 1 18:01:24 2022
https://pastebin.com/XAfGA0iY
import urllib.request
url = "
https://admin.quic.cloud/api/dnslist"
contents = urllib.request.urlopen(url).read().decode('utf-8')
# 得到每一行
str_contents = contents.splitlines()
# 用网页上的 空白 短杠 空白 切割每一行
parsing = lambda s:s.split(' - ')
# DNS_dict: python dictionary
# DNS名字当作 key
# IP位置当作 value
DNS_dict = dict( [ *map(parsing, str_contents) ] )
print( DNS_dict )
======================================================
各位前辈好。
下面的程式码可以执行
但我总得感觉起来没有利落的感觉
好像用任何程式语言都可以写出类似的东西
可以请前辈指点一二。
希望将网页的结果转成单纯的 temp dict
import urllib.request
url = "
https://admin.quic.cloud/api/dnslist"
contents = urllib.request.urlopen(url).read().decode('utf-8')
str_contents = contents.split('\n')
temp = dict()
for i in str_contents:
if i == '':
continue
cdn, ip = i.split(' - ')
temp[cdn] = ip
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.110.136.50 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1669885744.A.2F0.html
※ 编辑: wadd (140.110.136.50 台湾), 12/01/2022 17:15:43
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 114.37.167.185 (台湾)
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1669888886.A.AF6.html
1F:推 wadd: 太谢谢,我要研究一下相关的用法,这才是漂亮的程式码 12/01 19:00
2F:→ cuteSquirrel: 不客气 :) 12/01 19:05
3F:推 wadd: 还贴心的把方法都写出来,原来是map可以对每个item 12/01 19:06
4F:→ wadd: 执行相同的function 12/01 19:06