使用嵌套的 while 循环而不是 for 循环

using nested while loop instead of for loops

我为自己设计了一个编码挑战

我想更改嵌套列表的值 在不创建新列表的情况下使用 while 循环

此函数适用于单行嵌套列表,请参见下面的示例

基本上它的工作方式如下: 开始循环检查第一个值是否为特定类型,如果是则替换该值 退出循环,重新开始循环--> 第一个值已经被替换,所以索引被更新
并且它在下一个值之前,直到所有值都被替换

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")

我想扩展该功能,以便它可以用多行和多列替换嵌套列表

输出应该是这样的

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]]

这里我有点卡住了

我的伪代码:

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

到目前为止的方法,没有按预期工作

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

问题是如何在外循环中更新行索引,并使用更新后的行索引启动内循环?

单行列表示例


listoflist = [[None, None, None]]

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

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

你可以这样做:

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)

这给你这个结果

[['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']]

您遇到索引问题,并且在将新元素添加到列表后没有中断 while