本科毕业设计求助:多维数组拆分操作技术咨询
Got it, let's break down how to solve this problem—you've got a main array with 12 sub-arrays, each holding exactly 68 elements, and you want to extract elements by their position across all sub-arrays into separate arrays (e.g., all first elements go into one array, all second elements into another, etc.).
Here are practical examples in two common languages to help you out:
Python Implementation
Python's built-in zip() function is perfect for this kind of positional grouping. It takes multiple iterables and pairs up elements by their index:
# Example structure of your main array main_array = [ [s1_e1, s1_e2, ..., s1_e68], [s2_e1, s2_e2, ..., s2_e68], # ... up to 12 sub-arrays ] # Split into 68 arrays, each containing 12 elements (one from each sub-array) split_arrays = [list(group) for group in zip(*main_array)] # Access results: # split_arrays[0] = [s1_e1, s2_e1, ..., s12_e1] (all first elements) # split_arrays[1] = [s1_e2, s2_e2, ..., s12_e2] (all second elements) # ... and so on up to split_arrays[67]
The * operator unpacks your main array into 12 separate sub-arrays for zip() to process. Each group from zip() is a tuple of elements at the same index across sub-arrays, which we convert to a list for your final result.
JavaScript Implementation
If you're working in JS, you can initialize empty arrays and populate them by iterating through each sub-array:
// Example main array structure const mainArray = [ [s1_e1, s1_e2, ..., s1_e68], [s2_e1, s2_e2, ..., s2_e68], // ... 12 total sub-arrays ]; // Create 68 empty arrays to hold the split elements const splitArrays = Array.from({ length: 68 }, () => []); // Iterate through each sub-array and map elements to their target arrays mainArray.forEach(subArray => { subArray.forEach((element, index) => { splitArrays[index].push(element); }); }); // Use results like: // splitArrays[0] = [s1_e1, s2_e1, ..., s12_e1] // splitArrays[1] = [s1_e2, s2_e2, ..., s12_e2]
This approach explicitly loops through each element and places it in the corresponding array based on its index.
If your actual data has edge cases (like unexpected element types, or if you need to handle non-uniform sub-array lengths), sharing a sample snippet of your data would let me refine this solution even more!
内容的提问来源于stack exchange,提问作者user8100689

