Home > Enterprise >  How to create a 2d nested list from a text file using python?
How to create a 2d nested list from a text file using python?

Time:11-18

I'm a beginner programmer, and I'm trying to figure out how to create a 2d nested list (grid) from a particular text file. For example, the text file would look like this:

3
3
150
109
80
892
123
982
0
98
23

The first two lines in the text file would be used to create the grid, meaning that it is 3x3. The next 9 lines would be used to populate the grid, with the first 3 making up the first row, the next 3 making up the middle row, and the final 3 making up the last row. So the nested list would look like this:

[[150, 109, 80] [892, 123, 982] [0, 98, 23]]

How do I go about doing this? I was able to make a list of all of the contents, but I can't figure out how to use the first 2 lines to define the size of the inner lists within the outer list:

lineContent = []
innerList = ?
     for lines in open('document.txt','r'):
     value = int(lines)
     lineContent.append(value)

From here, where do I go to turn it into a nested list using the given values on the first 2 lines?

Thanks in advance.

CodePudding user response:

You can make this quite neat using list comprehension.

def txt_grid(your_txt):    
    with open(your_txt, 'r') as f:
        # Find columns and rows
        columns = int(f.readline())
        rows = int(f.readline())
        your_list = [[f.readline().strip() for i in range(rows)] for j in range(columns)]
    return your_list

print(txt_grid('document.txt'))

strip() just clears the newline characters (\n) from each line before storing them in the list.

Edit: A modified version with logic for if your txt file didn't have enough rows for the defined dimensions.

def txt_grid(your_txt):    
    with open(your_txt, 'r') as f:
        # Find columns and rows
        columns = int(f.readline())
        rows = int(f.readline())
        dimensions = columns * rows

        # Test to see if there are enough rows, creating grid if there are
        nonempty_lines = len([line.strip("\n") for line in f]) # This ignores the first two lines as they have already been written
        if nonempty_lines < dimensions:
            # Either raise an error
            # raise ValueError("Insufficient non-empty rows in text file for given dimensions")
            # Or return something that's not a list
            your_list = None
        else:
            # Creating grid
            your_list = [[f.readline().strip() for i in range(rows)] for j in range(columns)]
    return your_list

print(txt_grid('document.txt'))

CodePudding user response:

def parse_txt(filepath):
    lineContent = []
    with open(filepath, 'r') as txt:  # The with statement closes the txt file after its been used
        nrows = int(txt.readline())
        ncols = int(txt.readline())
        for i in range(nrows): # For each row
            row = []
            for j in range(ncols):  # Grab each value in the row
                row.append(int(txt.readline()))
            lineContent.append(row)
    return lineContent

grid_2d = parse_txt('document.txt')

CodePudding user response:

lineContent = []
innerList = []
for lines in open('testQuestion.txt', 'r'):
    value = int(lines)
    lineContent.append(value)

rowSz = lineContent[0]  # row size
colSz = lineContent[1]  # column size
del lineContent[0], lineContent[0]  # makes line contents just the values in the matrix, could also just start currentLine at 2, notice 0 index is repeated because 1st element was deleted 

assert rowSz * colSz == len(lineContent), 'not enough values for array' # to ensure there are enough entries to complete array of rowSz * colSz elements

arr = []
currentLine = 0
for x in range(rowSz):
    arr.append([])
    for y in range(colSz):
        arr[x].append(lineContent[currentLine])
        currentLine  = 1

print(arr)
  • Related