英文:
Appending array in a cycle
问题
你可以通过以下方式来修改你的代码,以便将所有数据存储在数组中,并为数组的列添加标题:
import numpy as np
result_primary = ['00000000', '01010101', '10101010', '11111111']
result_compressed = ['01100110', '10011001']
# 创建一个空的列表,用于存储结果
result_array = []
def hamming2(s1, s2):
assert len(s1) == len(s2)
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
# 添加标题到结果列表
result_array.append(["Primary", "Compressed", "Hamming Distance"])
# 使用嵌套循环计算Hamming距离并将结果添加到列表中
for i in result_primary:
for j in result_compressed:
hamming_distance = hamming2(i, j)
result_array.append([i, j, str(hamming_distance)])
# 打印结果数组,每行用制表符分隔
for row in result_array:
print('\t'.join(row))
这将给你以下输出,并将所有的数据存储在result_array
中:
Primary Compressed Hamming Distance
00000000 01100110 4
00000000 10011001 4
01010101 01100110 4
01010101 10011001 4
10101010 01100110 4
10101010 10011001 4
11111111 01100110 4
11111111 10011001 4
现在,你可以进一步操作result_array
中的数据。
英文:
I have two lists like in the example below:
result_primary = ['00000000', '01010101', '10101010', '11111111']
result_compressed = ['01100110', '10011001']
I am calculating the hamming distance between each element from the second list and each element from the first. For that purpose I have the following code:
def hamming2(s1, s2):
assert len(s1) == len(s2)
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
for i in result_primary:
for j in result_compressed:
primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)] )
print(primary_att_vs_compr_hamming)
This gives me the desired outcome at the console:
['00000000' '01100110' '4']
['00000000' '10011001' '4']
['01010101' '01100110' '4']
['01010101' '10011001' '4']
['10101010' '01100110' '4']
['10101010' '10011001' '4']
['11111111' '01100110' '4']
['11111111' '10011001' '4']
But actually I do not have all the data captured in the array, instead the cycle overwrites the variable in each iteration and at the end I have only the last row written in the array:
def hamming2(s1, s2):
assert len(s1) == len(s2)
return sum(c1 != c2 for c1, c2 in zip(s1, s2))
for i in result_primary:
for j in result_compressed:
primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)] )
print(primary_att_vs_compr_hamming)
Output:
['11111111' '10011001' '4']
How can I make it to write everything in the array (as I am willing to have it for further manipulations? Also, how can I put titles of the columns in the array (anyway it is array of strings)?
答案1
得分: 0
使用外部列表来存储结果:
data = []
for i in result_primary:
for j in result_compressed:
primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)])
data.append(primary_att_vs_compr_hamming)
输出:
>>> np.array(data)
array([['00000000', '01100110', '4'],
['00000000', '10011001', '4'],
['01010101', '01100110', '4'],
['01010101', '10011001', '4'],
['10101010', '01100110', '4'],
['10101010', '10011001', '4'],
['11111111', '01100110', '4'],
['11111111', '10011001', '4']], dtype='<U21')
英文:
Use an external list to store results:
data = []
for i in result_primary:
for j in result_compressed:
primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)])
data.append(primary_att_vs_compr_hamming)
Output:
>>> np.array(data)
array([['00000000', '01100110', '4'],
['00000000', '10011001', '4'],
['01010101', '01100110', '4'],
['01010101', '10011001', '4'],
['10101010', '01100110', '4'],
['10101010', '10011001', '4'],
['11111111', '01100110', '4'],
['11111111', '10011001', '4']], dtype='<U21')
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论