- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何在 python 中优化搜索两个元组中的大型 tsv 文件?
你好。我是一个 python 新手,一直致力于使用两个单独的元组来搜索匹配的元组元素。我使用的文件最多有 3M 行,但速度非常慢。我已经阅读帖子数周了,但似乎没有正确地将代码拼凑在一起。这是我到目前为止所拥有的。 (为了清晰起见,数据已被编辑和简化)。举例来说,我有:
authList = (jennifer, 35, 20),(john, 20, 34), (fred, 34, 89) # this is a tuple of
#unique tweet authors with their x, y coordinates exported from MS Access in the form
#of a txt file.
rtAuthors = (larry, 57, 24, simon), (jeremy, 24, 15, john), (sandra, 39, 24, fred)
# this is a tuple of tuples including the author, their x,y coordinates, and the
#author whom they are retweeting (taken from the "RT @ portion of their tweet)
我正在尝试创建一个新的元组 (rtAuthList),它从 rtAuthors 中任何转发的作者的 authList 中提取 x、y 坐标。
所以我会有一个像这样的新元组:
rtAuthList = (jeremy, 24, 15, john, 20, 34),(sandra, 39, 24, fred, 34, 89)
我的问题确实有两个部分,所以我不确定是否应该发布两个问题或重新命名我的问题以包含两个问题。首先,按照我编写的方式运行此过程大约需要一个小时。一定有更快的方法。
我的问题的另一部分是为什么它只完成了最终元组的大约一半?对于我当前的数据集,经过这两个步骤后,authList 中有大约 250,000 行,rtAuthors 中有 500,000 行。但是当我处理第三步并在最后打开 rtAuthList 时,它只查看了我的前 10 天的数据,忽略了最后 20 天——我正在处理一个月的推文)。我不知道为什么它没有检查整个 rtAuthors 列表。
我在下面包含了我的整个代码,以便您了解我想要做什么,但在创建 authList 和 rtAuthors 元组之后,我确实在步骤 3 中寻求帮助。请理解,我对编程完全陌生,所以写下答案就好像我什么都不知道一样,尽管当您查看我的代码时这可能是显而易见的。
import csv
import sys
import os
authors= ""
class TwitterFields: ### associated with monthly tweets from Twitter API
def __init__(self, ID, COORD1, COORD2,TIME, AUTH, TEXT):
self.ID = ID
self.COORD1 = COORD1
self.COORD2 = COORD2
self.TIME = TIME
self.AUTH=AUTH
self.TEXT=TEXT
self.RTAUTH=""
self.RTX=""
self.RTY=""
description="Twitter Data Class: holds twitter data fields from API "
author=""
class AuthorFields: ## associated with the txt file exported from MS Access
def __init__(self, AUTH, COORD1, COORD2):
self.AUTH=AUTH
self.COORD1 = COORD1
self.COORD2 = COORD2
self.RTAUTH=""
self.RTX=""
self.RTY=""
description="Author Data Class: holds author data fields from MS Access export"
author=""
tw = [] #empty list to hold data from class TwitterFields
rt = [] #empty list to hold data from class AuthorFields
authList = () ## tuple for holding auth, x, and y from tw list
rtAuthors = () ## tuple for holding tuples from rt where "RT @" is in tweet text
rtAuthList =() ## tuple for holding results of set intersection
e = () # tuple for authList
b=() # tuple for rtAuthors
c=() # tuple for rtAuthList
bad_data = [] #A container for bad data
with open(r'C:\Users\Amy\Desktop\Code\Merge2.txt') as g: #open MS Access export file
for line in g:
strLine = line.rstrip('\r\n').split("\t")
tw.append(AuthorFields( str(strLine[0]), #reads author name
strLine[1], # x coordinate
strLine[2])) # y coordinate
## Step 1 ##
# Loop through the unique author dataset (tw) and make a list of all authors,x, y
try:
for i in range(1, len(tw)):
e=((tw[i].AUTH[:tw[i].AUTH.index(" (")], tw[i].COORD1,tw[i].COORD2))
authList = authList +(e,)
except:
bad_data.append(i)
print "length of authList = ", len(authList)
# Loop through tweet txt file from MS Access
with open(r'C:\Users\Amy\Desktop\Code\Syria_2012_08UTCedits3.txt') as f:
for line in f:
strLine=line.rstrip('\r\n').split('\t') # parse each line for tab spaces
rt.append(TwitterFields(str(strLine[0]) , #reads tweet ID
strLine[5], # x coordinate
strLine[6], # y coordinate
strLine[8], # time stamp
strLine[9], # author
strLine[12] )) # tweet text
## Step 2 ##
## Loop through new list (rt) to find all instances of "RT @" and retrieve author name
for i in range(1, len(rt)): # creates tuple of (authors, x, y, rtauth, rtx, rty)
if (rt[i].TEXT[:4] == 'RT @'): # finds author in tweet text between "RT @" and ":"
end = rt[i].TEXT.find(":")
rt[i].RTAUTH=rt[i].TEXT[4:end]
b = ((rt[i].AUTH, rt[i].COORD1, rt[i].COORD2, rt[i].TIME, rt[i].RTAUTH))
rtAuthors = rtAuthors + (b,)
else:
pass
print "length of rtAuthors = ", len(rtAuthors)
## Step 3 ##
## Loop through new rtAuthors tuple and find where rt[i].RTAUTH matches tw[i].AUTH in
## authList.
set1 = set(k[4] for k in rtAuthors).intersection(x[0] for x in authList)
#e = iter(set1).next()
set2 = list(set1)
print "Length of first set = ", len(set2)
# For each match, grab the x and y from authList and copy to rt[i].RTX and rt[i].RTY
for i in range(1, len(rtAuthors)):
if rt[i].RTAUTH in set2:
authListIndex = [x[0] for x in authList].index(rt[i].RTAUTH) #get record #
rt[i].RTX= authList[authListIndex][1] # grab the x
rt[i].RTY = authList[authListIndex][2] # grab the y
c = ((rt[i].AUTH, rt[i].COORD1, rt[i].COORD2, rt[i].TIME, rt[i].RTAUTH,
rt[i].RTX, rt[i].RTY))
rtAuthList = rtAuthList + (c,) # create new tuple of tuples with matches
else:
pass
print "length of rtAuthList = ", len(rtAuthList)
最佳答案
在第 3 步中,您将使用 O(n²) 算法来匹配元组。如果您为 authList
构建查找字典,则可以在 O(n) 时间内完成...
>>> authList = ('jennifer', 35, 20), ('john', 20, 34), ('fred', 34, 89)
>>> rtAuthors = ('larry', 57, 24, 'simon'), ('jeremy', 24, 15, 'john'), ('sandra', 39, 24, 'fred')
>>> authDict = {t[0]: t[1:] for t in authList}
>>> rtAuthList = [t + authDict[t[-1]] for t in rtAuthors if t[-1] in authDict]
>>> print rtAuthList
[('jeremy', 24, 15, 'john', 20, 34), ('sandra', 39, 24, 'fred', 34, 89)]
关于python - 如何在 python 中优化搜索两个元组中的大 tsv 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17219831/
我在我的应用程序中使用 Hibernate Search。其中一个子集合被映射为 IndexedEmbedded。子对象有两个字段,一个是 id,另一个是日期(使用日期分辨率到毫秒)。当我搜索 id=
The App Engine Search API有一个 GeoPoint 字段。可以用它来进行半径搜索吗?例如,给定一个 GeoPoint,查找位于特定半径内的所有文档。 截至目前,它看起来像 Ge
客户对我正在做的员工管理项目提出了这个新要求,以允许他们的用户进行自定义 bool 搜索。 基本上允许他们使用:AND、OR、NOT、括号和引号。 实现它的最佳方法是什么?我检查了 mysql,它们使
很想知道哪个更快 - 如果我有一个包含 25000 个键值对的数组和一个包含相同信息的 MySQL 数据库,搜索哪个会更快? 非常感谢大家! 最佳答案 回答这个问题的最好方法是执行基准测试。 关于ph
我喜欢 smartcase,也喜欢 * 和 # 搜索命令。但我更希望 * 和 # 搜索命令区分大小写,而/和 ?搜索命令遵循 smartcase 启发式。 是否有隐藏在某个地方我还没有找到的设置?我宁
我有以下 Marklogic 查询,当在查询控制台中运行时,它允许我检索具有管理员权限的系统用户: xquery version "1.0-ml"; import schema namespace b
我希望当您搜索例如“A”时,所有以“A”开头的全名都会出现。因此,如果名为“Andreas blabla”的用户将显示 我现在有这个: $query = "SELECT full_name, id,
我想在我的网站上添加对人名的搜索。好友列表已经显示在页面上。 我喜欢 Facebook 这样做的方式,您开始输入姓名,Facebook 只会显示与查询匹配的好友。 http://cl.ly/2t2V0
您好,我在我的网站上进行搜索时遇到此错误。 Fatal error: Uncaught Error: Call to undefined function mysql_connect() in /ho
声明( 叠甲 ):鄙人水平有限,本文为作者的学习总结,仅供参考。 1. 搜索介绍 搜索算法包括深度优先搜索(DFS)和广度优先搜索(BFS)这两种,从起点开始,逐渐扩大
我正在为用户列表使用 FuturBuilder。我通过 futur: fetchpost() 通过 API 获取用户。在专栏的开头,我实现了一个搜索栏。那么我该如何实现我的搜索栏正在搜索呢? Cont
我正在使用 MVC5,我想搜索结果并停留在同一页面,这是我在 Controller (LiaisonsProjetsPPController) 中执行搜索操作的方法: public ActionRes
Azure 搜索中的两种方法 Upload 与 MergeOrUpload 之间有什么区别。 他们都做完全相同的事情。即,如果文档不存在,它们都会上传文档;如果文档已经存在,则替换该文档。 由于这两种
实际上,声音匹配/搜索的当前状态是什么?我目前正在远程参与规划一个 Web 应用程序,该应用程序将包含和公开记录的短音频剪辑(最多 3-5 秒,人名)的数据库。已经提出了一个问题,是否可以实现基于用户
在商业应用程序中,具有数百个面并不罕见。当然,并非所有产品都带有所有这些标记。 但是在搜索时,我需要添加一个方面查询字符串参数,其中列出了我想要返回的所有方面。由于我事先不知道相关列表,因此我必须在查
当我使用nvcc 5.0编译.cu文件时,编译器会为我提供以下信息。 /usr/bin/ld: skipping incompatible /usr/local/cuda-5.0/lib/libcud
我正在使用基于丰富的 Lucene 查询解析器语法的 Azure 搜索。我将“~1”定义为距离符号的附加参数)。但我面临的问题是,即使存在完全匹配,实体也没有排序。 (例如,“blue~1”将返回“b
我目前有 3 个类,一个包含 GUI 的主类,我在其中调用此方法,一个包含数据的客户类,以及一个从客户类收集数据并将其放入数组列表的 customerList 类,以及还包含搜索数组列表方法。 我正在
假设我有多个 6 字符的字母数字字符串。 abc123、abc231、abc456、cba123、bac231 和 bac123 。 基本上我想要一个可以搜索和列出所有 abc 实例的选择语句。 我只
我有这个表 "Table"内容: +--------+ | Serial | +--------+ | d100m | <- expected result | D100M | <- expect
我是一名优秀的程序员,十分优秀!