2

I did not get this question right. The function read_words turns a text file with a bunch of names in new lines into a list and it works.

def read_words(words_file):
    """ (file open for reading) -> list of str
    Return a list of all words (with newlines removed) from open file
    words_file.
    Precondition: Each line of the file contains a word in uppercase characters
    from the standard English alphabet.
    """
    words_list = []
    words = words_file.readlines()
    for i in range(len(words)):
        new_word = words[i]
        for char in '\n':
            new_word = new_word.replace(char,'')
            words_list.append(new_word)
    return words_list

the problem arises when I try to get a list of lists

def read_board(board_file):
    """ (file open for reading) -> list of list of str
    Return a board read from open file board_file. The board file will contain
    one row of the board per line. Newlines are not included in the board.
    """
    board_list = []
    row_list = []
    rows = read_words(board_file)
    for i in range(len(rows)):
        for  char in rows[i]:
            row_list.append(char)
        board_list.append(row_list)
    return board_list

the goal is to turn a text file of the type:

ABCD
EFGH

into [['A','B','C','D'],['E','F','G','H']]

I have already tried playing around with the indices for board_list.append(row_list) call without luck. How can I get this to work?

1
  • 3
    [list(line) for line in file]? Commented Mar 4, 2018 at 22:02

1 Answer 1

3

You can do that with a list comprehension and .strip() like:

Code:

def read_board(board_file):
    return [list(line.strip()) for line in read_words(board_file)]

Test Code:

def read_words(words_file):
    """ (file open for reading) -> list of str
    Return a list of all words (with newlines removed) from open file
    words_file.
    Precondition: Each line of the file contains a word in uppercase characters
    from the standard English alphabet.
    """
    return [word.strip() for word in words_file.readlines()]

def read_board(board_file):
    """ (file open for reading) -> list of list of str
    Return a board read from open file board_file. The board file will contain
    one row of the board per line. Newlines are not included in the board.
    """
    return [list(line) for line in read_words(board_file)]

with open('file1', 'rU') as f:
    board = read_board(f)

print(board)

Results:

[['A', 'B', 'C', 'D'], ['E', 'F', 'G', 'H']]
Sign up to request clarification or add additional context in comments.

3 Comments

If you strip the lines in read_words, why do you strip them again in read_board?
its... beautiful. Also i didn't know you could call return and for loop in the same line. I'm a changed man.
@GuriqbalChouhan List comprehension will definitely change the way you look at code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.