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. 字符数组 & 字符串

    字符数组 char c1[] = "ch111";        \\字符串字面值初始化.!!!字符串字面值末尾处有个\0空字符,也会被copy到字符数组中去,记得预留空间. ch ...

  2. springboot从入门到精通

    1:安装iDEa 2:安装jdk1.8 安装软件https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133 ...

  3. 线段树模板hdu 1754:I Hate It

    I Hate It Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  4. 在Windows 操作系统中, MySql 如何设置, 允许表名支持大小写

    一般在网上会说明 修改my.ini文件的  lower_case_table_names = 0 参照: http://www.linuxidc.com/Linux/2013-04/82719.htm ...

  5. parcel 中小型项目打包工具

    "0配置"打包器(bundler)Parcel Parcel官网(有中文文档) webpack 要有大量的配置,这样带来的成本就是复杂性--与此相对的,Parcel 带来了简洁性. ...

  6. Linux之临时配置网络(ip,网关,dns)+永久配置

    作业一:临时配置网络(ip,网关,dns)+永久配置 配置网络信息 [root@localhost ~]# ifconfig eno16777736: flags=4163<UP,BROADCA ...

  7. jQuery中动画常用的样式获取并改变方法-

    (1)Top 和 left 经常要用到jquery获取对象的位置,jquery top left,jquery css left是相对于父级元素中第一个position为relative或absolu ...

  8. SQL批量更新数据

    SQL批量更新数据 step1:导入Excel数据, 具体见百度.注意点:一列中含有float型数据和文本数据的时候,导入要将Excel中的表格属性改成文本,或在数字项目前加个单引号.   step2 ...

  9. TCP 粘包问题浅析及其解决方案

    最近一直在做中间件相关的东西,所以接触到的各种协议比较多,总的来说有TCP,UDP,HTTP等各种网络传输协议,因此楼主想先从协议最基本的TCP粘包问题搞起,把计算机网络这部分基础夯实一下. TCP协 ...

  10. 使用python实现深度神经网络 2(转)

    https://blog.csdn.net/oxuzhenyi/article/details/73026796 导数与梯度.矩阵运算性质.科学计算库numpy 一.实验介绍 1.1 实验内容 虽然在 ...