-1

I've coded a program that gives me data in tabular form (using tabulate) in python:

from tabulate import tabulate

matrix=[[1,2,3,4],[5,6,7,8]]

print(tabulate(matrix))

It gives me this output:

image

Now say that I want to split this table in 2 such that it gives:

image

How would I do that?

agg199
  • 43
  • 5
  • Please take the [tour], read [what's on-topic here](/help/on-topic), [ask], and the [question checklist](//meta.stackoverflow.com/q/260648/843953), and provide a [mre]. "Implement this feature for me" is off-topic for this site. You have to _make an honest attempt_, and then ask a _specific question_ about your algorithm or technique. – Pranav Hosangadi Nov 12 '20 at 20:50

1 Answers1

0

Here is a function I found here to split a normal array into two arrays. In the for loop, we split every one of the arrays in the matrix with the function. When you want to get the first part change the 1 to a 0.

from tabulate import tabulate

def split_list(a_list):
    half = len(a_list)//2
    return a_list[:half], a_list[half:]

matrix=[[1,2,3,4],[5,6,7,8]]
new_matrix = []

for arr in matrix:
    new_matrix.append(split_list(arr)[1]) #Replace this 1 with a 0

print(tabulate(new_matrix))

or just

from tabulate import tabulate

def split_list(a_list):
    half = len(a_list)//2
    return a_list[:half], a_list[half:]

matrix=[[1,2,3,4],[5,6,7,8]]
new_matrix = split_list(matrix[1])


print(tabulate(new_matrix))
Frederick
  • 452
  • 4
  • 19