追加数组循环中

huangapple go评论61阅读模式
英文:

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:

&gt;&gt;&gt; np.array(data)
array([[&#39;00000000&#39;, &#39;01100110&#39;, &#39;4&#39;],
       [&#39;00000000&#39;, &#39;10011001&#39;, &#39;4&#39;],
       [&#39;01010101&#39;, &#39;01100110&#39;, &#39;4&#39;],
       [&#39;01010101&#39;, &#39;10011001&#39;, &#39;4&#39;],
       [&#39;10101010&#39;, &#39;01100110&#39;, &#39;4&#39;],
       [&#39;10101010&#39;, &#39;10011001&#39;, &#39;4&#39;],
       [&#39;11111111&#39;, &#39;01100110&#39;, &#39;4&#39;],
       [&#39;11111111&#39;, &#39;10011001&#39;, &#39;4&#39;]], dtype=&#39;&lt;U21&#39;)

huangapple
  • 本文由 发表于 2023年2月10日 06:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404978.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定