2017-06-15 38 views
0

我有一個包含每行一組數字像這樣的文件:如何計算一個文件中許多數字的漢明重量?

[222 9 217 21 65 197] 
[222 9 217 21 65 197] 
[222 207 217 21 65 197] 
[ 9 222 217 21 65 197] 
[222 117 21 65 217 197] 
[222 117 21 65 217 197] 
[222 117 21 65 217 197] 
[222 117 21 65 217 197] 
........ 

我要計算每個號碼結果的漢明重量我想有:

[6 2 5 3 2 4] 
[6 2 5 3 2 4] 
[6 6 5 3 2 4] 
[2 6 5 3 2 4] 
[6 5 3 2 5 4] 
[6 5 3 2 5 4] 
[6 5 3 2 5 4] 
[6 5 3 2 5 3] 
........ 

我使用這個腳本來計算漢明重量:

hw = [bin(x).count("1") for x in range(256)] 
print(hw[207]) 

但我能做到這一點的只有一個號碼,我怎麼能做到這一點對所有的文件嗎?

可以幫我嗎?

+0

可以使用嵌套的for循環在列表理解 – Ding

回答

1

你可以試試這個:

from io import StringIO 

text = '''[222 9 217 21 65 197] 
[222 9 217 21 65 197] 
[222 207 217 21 65 197] 
[ 9 222 217 21 65 197] 
[222 117 21 65 217 197] 
[222 117 21 65 217 197] 
[222 117 21 65 217 197] 
[222 117 21 65 217 197]''' 


def hw(number): 
    ret = 0 
    while number != 0: 
     ret += number & 1 
     number >>= 1 
    return ret 
    # might be faster: 
    # return bin(number).count('1') 

# with open(..., 'r') as file 
with StringIO(text) as file: 
    for line in file: 
     line = line.strip() 
     line = line.replace('[', '') 
     line = line.replace(']', '') 
     numbers = [int(n) for n in line.split()] 
     hws = [hw(n) for n in numbers] 
     print(hws) 

,使其爲你工作,去掉上面的text部分,去掉with StringIO(text) as file:線並被with open(..., 'r') as file線替換它。你必須在那裏插入你的文件名(或更好的:你的文件的完整路徑)。

我還提供了不同版本的漢明重量;基於比特操作。

+0

另外,也可以使用一個正則表達式提取的數字,而不是所有'line'操作:'數= [INT(N )for n.in re.findall('\ d +',line)]''或者在re.findall('\ d +',line)中直接獲取'hws'作爲hws = [hw(int(n) ]'。 – JohanL

0

用於循環,並且不需要列表理解。

def hamAll(seq): 
    output = [] 
    for i in seq: 
    output.append(bin(i).count("1")) 
    return output 

殼:

>>> hamAll([1, 2, 3, 4, 5, 255]) 
...  #1 10 11 100 101 11111111 
[1, 1, 2, 1, 2, 8] 

然後,如果你想輸入一個文件,我會用re模塊。

import re 

def hamAll(seq): 
    output = [] 
    for i in seq: 
    output.append(bin(i).count("1")) 
    return output 

with open("filename.txt", 'r') as file: 
    inputf = file.read() 

inputf = re.sub(r"[\n\[\]]", " ", inputf) 
numbers = inputf.split(" ") 
hamReady = [] 
for i in numbers: 
    if i == "": 
    continue 
    if i.isdecimal(): 
    hamReady.append(int(i)) 

print(hamAll(hamReady)) 

殼:

======== Restart: test.py ======== 
[6, 2, 5, 3, 2, 4, 6, 2, 5, 3, 2, 4, 6, 6, 5, 3, 2, 4, 2, 6, 5, 3, 2, 4, 6, 5, 3, 2, 5, 4, 6, 5, 3, 2, 5, 4, 6, 5, 3, 2, 5, 4, 6, 5, 3, 2, 5, 4] 
>>> 
0

我選擇使用計算的漢明權重的一種方法,在這個答案中發現:https://stackoverflow.com/a/15540869/7938752

使用的組合與一些字符串格式化環從輸入文件中提取文本,提取數字,計算漢明重量,重新格式化數字,然後打印這些數字並將它們寫入輸出文件。

示例代碼:

def get_hm(x): 
    return bin(x).count("1") 

def format_numbers(results): 
    count = 0 
    result = "[" 
    for x in results: 
     if x >= 100: 
      result = result + str(x) 
     elif x >= 10: 
      result = result + " " + str(x) 
     else: 
      result = result + " " + str(x) 
     if count < len(results): 
      result = result + " " 
     count += 1 
    result = result + "]" 
    return result 

def get_numbers(line): 
    line = line.replace("[", "").replace("]","").replace("\n","") 
    results = [] 
    for x in line.split(" "): 
     if x != "" and x != " ": 
      results.append(get_hm((int(x)))) 
    return format_numbers(results) 


f = open("input.txt") 
y = f.readlines() 
for line in y: 
    print get_numbers(line) 
f.close() 

#optional code that writes the results to a file. 
f = open("output.txt", "w") 
for line in y: 
    f.write(get_numbers(line) + "\n") 
f.close() 
相關問題