M. Lont
M. Lont

Reputation: 13

Altering values of a list in a list

I'm trying to translate 'methuselahs' into binary code. All the dots ('.') should become 0, and all the O's ('O') should become 1.

I currently have a code that does work, but it will only return the first list of list_of_lists..

list_of_lists = [['.O'],['...O'],['OO..OOO']]

def read_file(list_of_lists):

     """Reads a file and returns a 2D list containing the pattern.
     O = Alive
     . = Dead
     """

     end_list = []
     sub_list = []

     for A_List in list_of_lists:
         for A_String in A_List:
             for item in A_String:

 #Adding 0's and 1's to sub_list when given the right input
                 if item == '.':

                     sub_list.append(0)

                 elif item == 'O':

                     sub_list.append(1)

 #Adding the sub
             end_list.append(sub_list)

         return end_list

Output :

[[0,1]]

But Expected Output:

[[0,1],[0,0,0,1],[1,1,0,0,1,1,1]]

Does anybody know how I can get the code to change all the lists instead of just the first one?

Upvotes: 1

Views: 80

Answers (3)

Sakib Ahammed
Sakib Ahammed

Reputation: 2480

Your code is ok. But problem in return end_list indentation level. When you return in for loop, after first iteration your function return and does not occur other iteration.

Try this, your code is modified:

list_of_lists = [['.O'],['...O'],['OO..OOO']]

def read_file(list_of_lists):

     """Reads a file and returns a 2D list containing the pattern.
     O = Alive
     . = Dead
     """

     end_list = []

     for A_List in list_of_lists:
         sub_list = []
         for A_String in A_List:
             for item in A_String:

 #Adding 0's and 1's to sub_list when given the right input
                 if item == '.':

                     sub_list.append(0)

                 elif item == 'O':

                     sub_list.append(1)

 #Adding the sub
             end_list.append(sub_list)

     return end_list

Output:

[[0,1],[0,0,0,1],[1,1,0,0,1,1,1]]

Upvotes: 0

Psytho
Psytho

Reputation: 3384

Outdent return end_list to the for A_List in list_of_lists: indentation level.

And bring sub_list = [] into that for-loop:

def read_file(list_of_lists):
    """Reads a file and returns a 2D list containing the pattern.
    O = Alive
    . = Dead
    """
    end_list = []
    for A_List in list_of_lists:
        sub_list = []
        for A_String in A_List:
            for item in A_String:
            #Adding 0's and 1's to sub_list when given the right input
                if item == '.':
                    sub_list.append(0)
                elif item == 'O':
                    sub_list.append(1)
            #Adding the sub
            end_list.append(sub_list)
    return end_list

Upvotes: 3

Anand S Kumar
Anand S Kumar

Reputation: 91009

Two issues in your code -

  1. You are returning from within the for loop, hence you return as soon as you finish the first sublist. Hence the output you are getting.

  2. You are not redefining sub_list within the for loop , without that there would only be one sub_list added multiple times and any changes you make to it would be reflected in all sublists.

But you don't need all this , you can use list comprehension to achieve the same thing -

def read_file(list_of_lists):
    return [[1 if ch == 'O' else 0 
             for st in sub_list for ch in st] 
             for sub_list in list_of_lists]

Demo -

>>> def read_file(list_of_lists):
...     return [[1 if ch == 'O' else 0
...              for st in sub_list for ch in st]
...              for sub_list in list_of_lists]
...
>>> read_file([['.O'],['...O'],['OO..OOO']])
[[0, 1], [0, 0, 0, 1], [1, 1, 0, 0, 1, 1, 1]]

Upvotes: 2

Related Questions