2014-03-02 19 views
1

我想將標準字典中的所有單詞(例如:/ usr/share/dict/unix機器的單詞)整數轉換並在每個單詞中找到XOR字典(將它們轉換爲整數後的字典),並可能將其存儲在一個新文件中。如何執行文件中所有單詞的異或

由於我是python的新手,並且由於文件較大,程序不時被掛起。

任何幫助將不勝感激。

import os 
dictionary = open("/usr/share/dict/words","r") 
'''a = os.path.getsize("/usr/share/dict/words") 
c = fo.read(a)''' 
words = dictionary.readlines() 

foo = open("word_integer.txt", "a") 


for word in words: 
    foo.write(word) 
    foo.write("\t") 
    int_word = int(word.encode('hex'), 16) 
    '''print int_word''' 
    foo.write(str(int_word)) 
    foo.write("\n") 

foo.close() 
+0

安置自己的代碼,以便我們可以幫助您解決 –

+0

我現在做到了,但我不知道如何重複遍歷所有單詞 – kingmakerking

+0

以下內容有幫助嗎?您使用的是什麼版本的Python? –

回答

2

首先,我們需要您的字符串轉換爲int的方法,我會做出一個了(因爲你的意思是編碼爲Unicode你在做什麼,不是我工作的所有,也許? ):

def word_to_int(word): 
    return sum(ord(i) for i in word.strip()) 

接下來,我們需要處理這些文件。在Python 2.7以後contextlib.nested下面的作品,(在2.6,只是窩兩個獨立的積木,或者使用:

with open("/usr/share/dict/words","rU") as dictionary: 
    with open("word_integer.txt", "a") as foo: 
     while dictionary: 
      try: 
       w1, w2 = next(dictionary), next(dictionary) 
       foo.write(str(word_to_int(w1)^word_to_int(w2))) 
      except StopIteration: 
       print("We've run out of words!") 
       break 
+0

@這個代碼會運行嗎?或者我應該添加它與我的代碼?我不理解。 當我嘗試單獨運行您的代碼時,它會說出語法錯誤並將 中的「as」指向(打開(「/ usr/share/dict/words」,「rU」)作爲詞典, – kingmakerking

+0

@ user2888239 This應該作爲上面代碼的直接替代品,記住要接受一個適合你的答案(答案旁邊的複選標記),你會得到+2代表只是爲了做到這一點。 '打開文件,它會自動處理關閉它們,如果你得到一個錯誤。 –

+0

這是它現在的樣子,但沒有運氣。我想我不知道如何使用它。import word def word_to_int(word):return sum (打開(「/ usr/share/dict/words」,「rU」)作爲字典,打開(「word_integer2.txt」,「a」)(從contextlib導入contextmanager @contextmanager) as foo):while dictionary:try:w1,w2 = next(dictionary),next(dictionary)foo.write(word_to_int(w1)^ w ord_to_int(w2)),除了StopIteration:print('我們已經用完了單詞!') – kingmakerking

0

此代碼似乎適用於我。您可能會遇到效率問題,因爲您在整個文件中調用readlines(),該文件立即將其全部存入內存。

該解決方案爲每行逐行掃描文件並計算xor。

f = open('/usr/share/dict/words', 'r')           

pairwise_xors = {}                

def str_to_int(w):                
    return int(w.encode('hex'), 16)            

while True:                  
    line1 = f.readline().strip()             
    g = open('/usr/share/dict/words', 'r')          
    line2 = g.readline().strip()             

    if line1 and line2:               
     pairwise_xors[(line1, line2)] = (str_to_int(line1)^str_to_int(line2)) 
    else:                  
     g.close()                
     break                 

f.close()    
+0

但是,如果我是對的,是不是在第7行的while循環中再次寫入/ usr/share/dict/words?如果我理解的邏輯是真的,我可以將其更改爲其他文件名。 – kingmakerking

+0

該代碼無效,可能是因爲它沒有寫任何東西 – kingmakerking

+0

你應該使用上下文管理器,你需要學習如何在迭代器上使用next。 –

相關問題