简体   繁体   中英

using nested while loop instead of for loops

I created a coding challenge for me

I want to change the values of a nested list using a while loop without creating a new list

this function works well with a single row nested list see example below

basically it works like: start the loop checks if first value is a specific type if so repaces the value go out of the loop, start the loop again--> the first value is already replaced so the index is updated
and it precedes with the next value till all values are replaced

def insertdata(data):
    data_added = False
    n = len(listoflist[0])
    index = 0

    while not data_added and index != n:
            if listoflist[0][index] is None:
                listoflist[0][index] = data
                data_added = True

            else:
                index += 1

            if index == n:
                print("\n The list is full, No more elements will be added \n")

I want to extend the function so it can replace nested lists with multyple rows and columns

the output should be somethinge like that

listoflist = [[None, None, None],[None, None, None]]

def _execute():
    user_input = input("type in: ")
    return user_input

while True:
  def insertdata(_execute()):


input = 2

[[2, None, None],[None, None, None]]

input = 4

[[2, 4, None],[None, None, None]]

here im little bit stuck

my pseudocode:

rowindex = 0
main loop 
 # start second loop 
  start second loop 
  # performs the operation for the row [rowindex][columnindex]
  # if all values replaced in the row
  # go back to main loop update the row count and procede with the next column

aproach so far, not working as expected

def insertdata(data):

    row_index = 0
    while row_index != len(listoflist):

       # inner loop
        data_added = False
        n = len(listoflist[row_index])
        index = 0

        while not data_added and index != n:
            if listoflist[row_index][index] is None:
                listoflist[row_index][index] = data
                data_added = True

            else:
                index += 1

            # gets not executed
            if index == n:
                row_index += 1
                break

problem is how to update the rowindex in the outer loop, and start the inner loop with the updated rowindex?

example for single row list


listoflist = [[None, None, None]]

def _execute():
    user_input = input("type in: ")
    return user_input

while True:
    insertdata(_execute())
    print(listoflist)

You can do this:

listoflist = [[None, None, None], [None, None, None]]

def insertdata(data):

    row_index = 0
    while row_index < len(listoflist):

       # inner loop
        data_added = False
        n = len(listoflist[row_index])
        index = 0

        while index < n:
            if listoflist[row_index][index] is None:
                listoflist[row_index][index] = data
                data_added = True
                break
            else:
                index += 1

        if index == n:
            row_index += 1

        if data_added:
          break


for i in range(7):
    insertdata(f'input {i}')
    print(listoflist)

This gives you this result

[['input 0', None, None], [None, None, None]]
[['input 0', 'input 1', None], [None, None, None]]
[['input 0', 'input 1', 'input 2'], [None, None, None]]
[['input 0', 'input 1', 'input 2'], ['input 3', None, None]]
[['input 0', 'input 1', 'input 2'], ['input 3', 'input 4', None]]
[['input 0', 'input 1', 'input 2'], ['input 3', 'input 4', 'input 5']]
[['input 0', 'input 1', 'input 2'], ['input 3', 'input 4', 'input 5']]

You were having issues with indexes and not breaking the while after a new element was added to the list

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM