sunlove521 发表于 2020-12-16 22:32

python 列表合并

['cnht.com.cn', '58.18.172.18-58.18.172.18'],
['cnht.com.cn', '116.113.45.66-116.113.45.78'],
['cnht.com.cn', '116.113.45.82-116.113.45.86'],
['cnht.com.cn', '202.99.230.130-202.99.230.142'],


如上内容,怎样根据域名合并,最终结果
{'cnht.com.cn': ['58.18.172.18-58.18.172.18','116.113.45.66-116.113.45.78','116.113.45.82-116.113.45.86','202.99.230.130-202.99.230.142']}


zxy877298415 发表于 2020-12-22 11:23

回复 1# sunlove521

b=[]
d={}
with open ('file1.txt','r') as f:
        for i in f:
                d.setdefault(eval(i.rstrip(",\n")),[]).append(eval(i.rstrip(",\n")))

print (d)

liangqing 发表于 2021-01-14 13:56

提供高质量代理IP 加QQ 2660766793

landihua 发表于 2021-06-07 16:21

import re
with open ('filename.txt','r') as f:
    dict1={}
    for line in f:
      line=re.match(r"^\[.*\]",line)
      domain=eval(line.group())
      ips=eval(line.group())
      dict1.setdefault(domain,[]).append(ips)
print(dict1)
页: [1]
查看完整版本: python 列表合并