簡體   English   中英

如何對文件中的所有單詞執行XOR

[英]how to perform XOR of all words in a file

我想轉換標准字典中的所有詞(例如:/ 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()

首先,我們需要一種將您的字符串轉換為int的方法,我將做一個補充(由於您的工作根本不適合我,也許您想編碼為unicode?):

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

接下來,我們需要處理文件。 以下在Python 2.7及更高版本中有效(在2.6中,僅將兩個帶塊的嵌套在一起,或使用contextlib.nested

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

該代碼似乎對我有用。 您可能會遇到效率問題,因為您要在整個文件上調用readlines() ,這會將其立即全部拉到內存中。

此解決方案逐行循環遍歷文件,並計算異或。

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()             

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM