import sys
import tstree fname = 'high_freq_site.list'
tree = tstree.TernarySearchTrie()
tree.loadData(fname) token = ''
counter =
post = [] # url, count, posttime
for line in sys.stdin:
line = line.strip()
arr = line.split()
if len(arr) != :
continue #print arr
num = arr[]
url = arr[]
posttime = int(arr[]) if token == '':
token = url
counter =
counter += int(num)
post.append(posttime)
elif token == url:
counter += int(num)
post.append(posttime)
elif token != url:
ret = tree.maxMatch(token)
if ret and post:
print '%s\t%s\t%s\t%s' % (ret, token, counter, min(post)) token = url
counter =
counter += int(num)
post = [] ret = tree.maxMatch(token)
if ret and post:
print '%s\t%s\t%s\t%s' % (ret, token, counter, min(post)) class TSTNode(object):
def __init__(self, splitchar):
self.splitchar = splitchar
self.data = None self.loNode = None
self.eqNode = None
self.hiNode = None class TernarySearchTrie(object):
def __init__(self):
self.rootNode = None def loadData(self, fname):
f = open(fname)
while True:
line = f.readline()
if not line:
break
line = line.strip()
node = self.addWord(line)
if node:
node.data = line
f.close() def addWord(self, word):
if not word:
return None charIndex =
if not self.rootNode:
self.rootNode = TSTNode(word[]) currentNode = self.rootNode while True:
charComp = ord(word[charIndex]) - ord(currentNode.splitchar)
if charComp == :
charIndex +=
if charIndex == len(word):
return currentNode
if not currentNode.eqNode:
currentNode.eqNode = TSTNode(word[charIndex])
currentNode = currentNode.eqNode
elif charComp < :
if not currentNode.loNode:
currentNode.loNode = TSTNode(word[charIndex])
currentNode = currentNode.loNode
else:
if not currentNode.hiNode:
currentNode.hiNode = TSTNode(word[charIndex])
currentNode = currentNode.hiNode def maxMatch(self, url):
ret = None
currentNode = self.rootNode
charIndex =
while currentNode:
if charIndex >= len(url):
break
charComp = ord(url[charIndex]) - ord(currentNode.splitchar)
if charComp == :
charIndex +=
if currentNode.data:
ret = currentNode.data
if charIndex == len(url):
return ret
currentNode = currentNode.eqNode
elif charComp < :
currentNode = currentNode.loNode
else:
currentNode = currentNode.hiNode
return ret if __name__ == '__main__':
import sys
fname = 'high_freq_site.list'
tree = TernarySearchTrie()
tree.loadData(fname) for url in sys.stdin:
url = url.strip()
ret = tree.maxMatch(url)
print ret

python 遍历hadoop, 跟指定列表对比 包含列表中值的取出。的更多相关文章

  1. 数据结构作业——P53算法设计题(6):设计一个算法,通过一趟遍历确定长度为n的单链表中值最大的结点

    思路: 设单链表首个元素为最大值max 通过遍历元素,与最大值max作比较,将较大值附给max 输出最大值max 算法: /* *title:P53页程序设计第6题 *writer:weiyuexin ...

  2. 使用python遍历指定城市的一周气温

    处于兴趣,写了一个遍历指定城市五天内的天气预报,并转为华氏度显示.把城市名字写到一个列表里这样可以方便的添加城市.并附有详细注释 1 import requests import json#定义一个函 ...

  3. python遍历列表删除多个元素的坑

    如下代码,遍历列表,删除列表中的偶数时,结果与预期不符. a = [11, 20, 4, 5, 16, 28] for i in a: if i % 2 == 0: a.remove(i) print ...

  4. python开发学习-day02(元组、字符串、列表、字典深入)

    s12-20160109-day02 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...

  5. Python黑帽编程2.3 字符串、列表、元组、字典和集合

    Python黑帽编程2.3  字符串.列表.元组.字典和集合 本节要介绍的是Python里面常用的几种数据结构.通常情况下,声明一个变量只保存一个值是远远不够的,我们需要将一组或多组数据进行存储.查询 ...

  6. python整理之(字符串、元组、列表、字典)

    一.关于字符串的整理总结 对于字符串的操作常用的有这些: 字符串的操作通过dir()函数可以查看 我们先整理没有下划线的用法,有下划线的暂时不去考虑. 1.capitalize 功能:使字符串的首字母 ...

  7. python基础知识3——基本的数据类型2——列表,元组,字典,集合

    磨人的小妖精们啊!终于可以归置下自己的大脑啦,在这里我要把--整型,长整型,浮点型,字符串,列表,元组,字典,集合,这几个知识点特别多的东西,统一的捯饬捯饬,不然一直脑袋里面乱乱的. 一.列表 1.列 ...

  8. Python第三天 序列 数据类型 数值 字符串 列表 元组 字典

    Python第三天 序列  数据类型  数值  字符串  列表  元组  字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...

  9. Python:list 和 array的对比以及转换时的注意事项

    Python:list 和 array的对比以及转换时的注意事项 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-6-4 ...

随机推荐

  1. ThinkPHP CI codeignitor 框架 apache 重写 url 隐藏index.php 服务器 报错:Object not found! 可能是.htaccess隐藏index.php

    隐藏index.php可以去掉URL地址里面的入口文件index.php,但是需要额外配置WEB服务器的重写规则.以Apache为例,需要在入口文件的同级添加.htaccess文件(官方默认自带了该文 ...

  2. [JOISC2014]スタンプラリー

    [JOISC2014]スタンプラリー 题目大意: 有\(n(n\le3000)\)个车站,另有一个起点站和终点站,所有车站排成一条链,相邻两个车站之间的距离为\(t\).每个车站都有一个上行站台.一个 ...

  3. [CF453B]Little Pony and Harmony Chest

    [CF453B]Little Pony and Harmony Chest 题目大意: 给你一个长度为\(n(n\le100)\)的正整数序列\(A(A_i\le30)\),求一个正整数序列\(B\) ...

  4. Java中的public、private、protected,函数修饰符

    1.public:public表明该数据成员.成员函数是对所有用户开放的,项目中其他脚本都可以直接进行调用 2.private:private表示私有,私有的意思就是除了脚本之外,项目中其他类都不可以 ...

  5. JS中原始类型Null和Undefined

    Undefined类型只有一个值,即undefined.当声明的变量还未被初始化时,变量的默认值为undefined.Null类型也只有一个值,即null.null用来表示尚未存在的对象,常用来表示函 ...

  6. 百度杯 ctf 九月场---Text

    一看题目发现善于查资料就行了,那估计就是以前的漏洞,需要百度搜一下,果然是海洋cms的漏洞!这个漏洞是前台getshell漏洞,seach漏洞,该漏洞成因在于search.php没有对用户输入内容进行 ...

  7. Django中提供的6种缓存方式

    由于Django是动态网站,所有每次请求均会去数据进行相应的操作,当程序访问量大时,耗时必然会更加明显,最简单解决方式是使用: 缓存,缓存将一个某个views的返回值保存至内存或者memcache中, ...

  8. python标准库大全(转)

    3. 清晰的标准库大全,带例子 2. 必会标准库 http://lizhenliang.blog.51cto.com/7876557/1872538 1. 标准库大全,链接版 http://blog. ...

  9. hihocoder1323 回文字符串(区间dp)

    https://hihocoder.com/problemset/problem/1323 刚开始真没看出来这是一道dp题.. dp[i][j]表示i~j段修改成回文串所需的最少操作次数.然后根据s[ ...

  10. 如何给webbrowser指定IE版本

    void Button1Click(object sender, EventArgs e)     {         RegistryKey rk = Registry.LocalMachine; ...