Chinaunix

标题: python 列表合并 [打印本页]

作者: 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"))[0],[]).append(eval(i.rstrip(",\n"))[1])

print (d)

作者: liangqing    时间: 2021-01-14 13:56
提供高质量代理IP 加QQ 2660766793
作者: landihua    时间: 2021-06-07 16:21
  1. import re
  2. with open ('filename.txt','r') as f:
  3.     dict1={}
  4.     for line in f:
  5.         line=re.match(r"^\[.*\]",line)
  6.         domain=eval(line.group())[0]
  7.         ips=eval(line.group())[1]
  8.         dict1.setdefault(domain,[]).append(ips)
  9. print(dict1)
复制代码





欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2