python 遍历hadoop, 跟指定列表对比 包含列表中值的取出。
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, 跟指定列表对比 包含列表中值的取出。的更多相关文章
- 数据结构作业——P53算法设计题(6):设计一个算法,通过一趟遍历确定长度为n的单链表中值最大的结点
思路: 设单链表首个元素为最大值max 通过遍历元素,与最大值max作比较,将较大值附给max 输出最大值max 算法: /* *title:P53页程序设计第6题 *writer:weiyuexin ...
- 使用python遍历指定城市的一周气温
处于兴趣,写了一个遍历指定城市五天内的天气预报,并转为华氏度显示.把城市名字写到一个列表里这样可以方便的添加城市.并附有详细注释 1 import requests import json#定义一个函 ...
- python遍历列表删除多个元素的坑
如下代码,遍历列表,删除列表中的偶数时,结果与预期不符. a = [11, 20, 4, 5, 16, 28] for i in a: if i % 2 == 0: a.remove(i) print ...
- python开发学习-day02(元组、字符串、列表、字典深入)
s12-20160109-day02 *:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: ...
- Python黑帽编程2.3 字符串、列表、元组、字典和集合
Python黑帽编程2.3 字符串.列表.元组.字典和集合 本节要介绍的是Python里面常用的几种数据结构.通常情况下,声明一个变量只保存一个值是远远不够的,我们需要将一组或多组数据进行存储.查询 ...
- python整理之(字符串、元组、列表、字典)
一.关于字符串的整理总结 对于字符串的操作常用的有这些: 字符串的操作通过dir()函数可以查看 我们先整理没有下划线的用法,有下划线的暂时不去考虑. 1.capitalize 功能:使字符串的首字母 ...
- python基础知识3——基本的数据类型2——列表,元组,字典,集合
磨人的小妖精们啊!终于可以归置下自己的大脑啦,在这里我要把--整型,长整型,浮点型,字符串,列表,元组,字典,集合,这几个知识点特别多的东西,统一的捯饬捯饬,不然一直脑袋里面乱乱的. 一.列表 1.列 ...
- Python第三天 序列 数据类型 数值 字符串 列表 元组 字典
Python第三天 序列 数据类型 数值 字符串 列表 元组 字典 数据类型数值字符串列表元组字典 序列序列:字符串.列表.元组序列的两个主要特点是索引操作符和切片操作符- 索引操作符让我 ...
- Python:list 和 array的对比以及转换时的注意事项
Python:list 和 array的对比以及转换时的注意事项 zoerywzhou@163.com http://www.cnblogs.com/swje/ 作者:Zhouwan 2017-6-4 ...
随机推荐
- Tapable.plugin is deprecated. Use new API on `.hooks` instead
问题描述 在使用extract-text-webpack-plugin给webpack打包时出现报错 Tapable.plugin is deprecated. Use new API on `.ho ...
- Java 动态代理 Demo
相比于静态代理,动态代理避免了开发者编写各个繁锁的静态代理类,只需指定一组接口及目标类对象就能动态地获取代理对象. 使用动态代理的六大步骤: 1 通过实现InvocationHandler接口来自定义 ...
- 深入理解JVM(10)——Class文件结构
什么是“JVM”的无关性 Java具有平台无关性,也就是任何操作系统都能够运行Java代码,之所以能够实现这一点,是因为Java运行在虚拟机上,不同的操作系统都有各自的Java虚拟机,从而实现一次编译 ...
- JS_高阶函数(sort)
//javaScript sort()排序算法 //sort()方法默认把所有的元素转换成String再排序,字符串是根据ASCII进行排序的,所以会出现“10”排在“2”前面,或是小写字母“a”排在 ...
- JS_高程4.变量,作用域和内存问题(2)执行环境及作用域
1.执行环境:执行环境定义了变量或函数有权访问的其他数据,决定了它们各自的行为, 每个执行环境都有一个与之相关联的变量对象,环境中定义的所有变量和函数都保存在这个对象中. 2.全局执行环境: 最外围的 ...
- Listener(1)—基础知识
一.监听器 1.概念: 专门用于对其他对象身上发生的事件或状态改变进行监听和相应处理的对象,当被监听的对象状态发生情况时,立即采取相应的 行动.Servlet规范为每种事件监听器都定义了相应的接口,w ...
- PowerBI发布到网页
如果网页当中需要嵌入PowerBI的报表,可以在PowerBI当中生成链接,然后网页或者博客当中插入这一段html代码. 以下是PowerBI生产网页链接的示例,并且在博客的最后也插入了PowerBI ...
- 自定义UIPickView
效果图 源码 https://github.com/YouXianMing/Animations 说明 1. 数据适配器PickerViewDataAdapter含有PickerViewCompone ...
- Sublime Text 显示韩文和文件编码
菜单 Preferences –> Settings 右侧的User { "font_size": 12, "show_encoding": ...
- [Web 前端] mobx教程(二)-mobx主要概念
cp from : https://blog.csdn.net/smk108/article/details/84960159 通过<Mobx教程(一)-Mobx简介>我们简单理解了Mob ...