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
import re
with open ('filename.txt','r') as f:
dict1={}
for line in f:
line=re.match(r"^\[.*\]",line)
domain=eval(line.group())[0]
ips=eval(line.group())[1]
dict1.setdefault(domain,[]).append(ips)
print(dict1)
复制代码
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2