You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何优化遗传代码算法输出:去除array标识并实现分行显示?

Fixing Numpy Array Output Formatting Issue

Got it, let's tackle this output formatting problem you're facing. The issue stems from two things: np.array_split returns a list of numpy arrays (hence the array() labels), and printing the list directly dumps everything on a single line. Here's how to fix both problems cleanly:

Updated Solution Code

import numpy as np

A = [1, 0, 0, 0]
C = [0, 1, 0, 0]
T = [0, 0, 1, 0]
G = [0, 0, 0, 1]
original = [A, T, T, G, C, G]
copy = [C, T, T, A, T, A]

# Your original list logic stays the same
x = [1 if 1 in j else 0 for i, v in enumerate(original) for j in zip(v, copy[i])]
new = np.array_split(x, len(original))

# Convert numpy arrays to regular lists and print each on a new line
for item in new:
    print(item.tolist())

Key Changes Explained

  • Remove array() labels: Calling .tolist() on each numpy array converts it to a standard Python list, which prints without the numpy-specific array() wrapper.
  • Line-by-line readability: Instead of printing the entire list at once, we loop through each element in new and print it individually. This ensures each result gets its own line for easy scanning.

Expected Output

[1, 1, 0, 0]
[0, 0, 1, 0]
[0, 0, 1, 0]
[1, 0, 0, 1]
[0, 1, 1, 0]
[1, 0, 0, 1]

If you prefer a one-liner instead of a loop, you can also use this:

print('\n'.join(map(str, [arr.tolist() for arr in new])))

内容的提问来源于stack exchange,提问作者Rishi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 07:37:56