追加数组循环中

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

Appending array in a cycle

问题

你可以通过以下方式来修改你的代码,以便将所有数据存储在数组中,并为数组的列添加标题:

  1. import numpy as np
  2. result_primary = ['00000000', '01010101', '10101010', '11111111']
  3. result_compressed = ['01100110', '10011001']
  4. # 创建一个空的列表,用于存储结果
  5. result_array = []
  6. def hamming2(s1, s2):
  7. assert len(s1) == len(s2)
  8. return sum(c1 != c2 for c1, c2 in zip(s1, s2))
  9. # 添加标题到结果列表
  10. result_array.append(["Primary", "Compressed", "Hamming Distance"])
  11. # 使用嵌套循环计算Hamming距离并将结果添加到列表中
  12. for i in result_primary:
  13. for j in result_compressed:
  14. hamming_distance = hamming2(i, j)
  15. result_array.append([i, j, str(hamming_distance)])
  16. # 打印结果数组,每行用制表符分隔
  17. for row in result_array:
  18. print('\t'.join(row))

这将给你以下输出,并将所有的数据存储在result_array中:

  1. Primary Compressed Hamming Distance
  2. 00000000 01100110 4
  3. 00000000 10011001 4
  4. 01010101 01100110 4
  5. 01010101 10011001 4
  6. 10101010 01100110 4
  7. 10101010 10011001 4
  8. 11111111 01100110 4
  9. 11111111 10011001 4

现在,你可以进一步操作result_array中的数据。

英文:

I have two lists like in the example below:

  1. result_primary = ['00000000', '01010101', '10101010', '11111111']
  2. 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:

  1. def hamming2(s1, s2):
  2. assert len(s1) == len(s2)
  3. return sum(c1 != c2 for c1, c2 in zip(s1, s2))
  4. for i in result_primary:
  5. for j in result_compressed:
  6. primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)] )
  7. print(primary_att_vs_compr_hamming)

This gives me the desired outcome at the console:

  1. ['00000000' '01100110' '4']
  2. ['00000000' '10011001' '4']
  3. ['01010101' '01100110' '4']
  4. ['01010101' '10011001' '4']
  5. ['10101010' '01100110' '4']
  6. ['10101010' '10011001' '4']
  7. ['11111111' '01100110' '4']
  8. ['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:

  1. def hamming2(s1, s2):
  2. assert len(s1) == len(s2)
  3. return sum(c1 != c2 for c1, c2 in zip(s1, s2))
  4. for i in result_primary:
  5. for j in result_compressed:
  6. primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)] )
  7. print(primary_att_vs_compr_hamming)

Output:

  1. ['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

使用外部列表来存储结果:

  1. data = []
  2. for i in result_primary:
  3. for j in result_compressed:
  4. primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)])
  5. data.append(primary_att_vs_compr_hamming)

输出:

  1. >>> np.array(data)
  2. array([['00000000', '01100110', '4'],
  3. ['00000000', '10011001', '4'],
  4. ['01010101', '01100110', '4'],
  5. ['01010101', '10011001', '4'],
  6. ['10101010', '01100110', '4'],
  7. ['10101010', '10011001', '4'],
  8. ['11111111', '01100110', '4'],
  9. ['11111111', '10011001', '4']], dtype='<U21')
英文:

Use an external list to store results:

  1. data = []
  2. for i in result_primary:
  3. for j in result_compressed:
  4. primary_att_vs_compr_hamming = np.array([i, j, hamming2(i, j)])
  5. data.append(primary_att_vs_compr_hamming)

Output:

  1. &gt;&gt;&gt; np.array(data)
  2. array([[&#39;00000000&#39;, &#39;01100110&#39;, &#39;4&#39;],
  3. [&#39;00000000&#39;, &#39;10011001&#39;, &#39;4&#39;],
  4. [&#39;01010101&#39;, &#39;01100110&#39;, &#39;4&#39;],
  5. [&#39;01010101&#39;, &#39;10011001&#39;, &#39;4&#39;],
  6. [&#39;10101010&#39;, &#39;01100110&#39;, &#39;4&#39;],
  7. [&#39;10101010&#39;, &#39;10011001&#39;, &#39;4&#39;],
  8. [&#39;11111111&#39;, &#39;01100110&#39;, &#39;4&#39;],
  9. [&#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:

确定