Skip to content

Instantly share code, notes, and snippets.

@aoshiman
Created November 7, 2014 03:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aoshiman/a3ddec9e5c829bab22c3 to your computer and use it in GitHub Desktop.
Save aoshiman/a3ddec9e5c829bab22c3 to your computer and use it in GitHub Desktop.
defaultdictを使ったtreeの作り方
# -*- coding: utf-8 -*-
import sys
from collections import defaultdict
from pprint import pprint
"""
defaultdictを使ったtreeの作り方
参考URL
http://sonickun.hatenablog.com/entry/2014/07/12/231443
https://gist.github.com/hrldcpr/2012250
"""
def tree():
return defaultdict(tree)
def dicts(t):
return {k: dicts(t[k]) for k in t}
def add(t, path):
for node in path:
t = t[node]
def make_a_tree(txt):
domain_tree = tree()
with open(txt, "r") as f:
for i in f:
row = i.replace("\n", "").split(".")
row.reverse()
add(domain_tree, row)
return dicts(domain_tree)
if __name__ == '__main__':
pprint(make_a_tree(sys.argv[1]))
@aoshiman
Copy link
Author

このスクリプトを使用して Adaway用広告除去リスト のdomain をtreeにしてみた
https://gist.github.com/aoshiman/c8513d191c7fca666b44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment