Pythonで大きなtsvファイルの2つのタプルの検索を最適化するにはどうすればよいですか?
こんにちは。私は python newb であり、2 つの別々のタプルを使用して一致するタプル要素を検索することに取り組んでいます。最大 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)
rtAuthors でリツイートされた作成者の authList から x、y 座標を取得する新しいタプル (rtAuthList) を作成しようとしています。
したがって、次のような新しいタプルがあります。
rtAuthList = (jeremy, 24, 15, john, 20, 34),(sandra, 39, 24, fred, 34, 89)
質問には実際に 2 つの部分があるので、2 つの質問を投稿するか、質問のタイトルを変更して両方を含めるかがわかりません。まず、このプロセスは、私が書いた方法で実行するのに約 1 時間かかります。もっと速い方法があるはずです。
私の質問の他の部分は、なぜ最終的なタプルの約半分しか完了していないのですか? 私の現在のデータセットでは、これらの 2 つの手順の後、authList に約 250,000 行、rtAuthors に 500,000 行あります。しかし、3 番目のステップを処理して最後に rtAuthList を開くと、最初の 10 日間のデータのみが表示され、最後の 20 日間は無視されます (1 か月分のツイートを処理しています)。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)