如何优化遗传代码算法输出:去除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-specificarray()wrapper. - Line-by-line readability: Instead of printing the entire list at once, we loop through each element in
newand 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
相关产品推荐
相关产品推荐

