繁体   English   中英

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

[英]using nested while loop instead of for loops

我为我创建了一个编码挑战

我想使用 while 循环更改嵌套列表的值而不创建新列表

这个 function 适用于单行嵌套列表,请参见下面的示例

基本上它的工作原理如下:启动循环检查第一个值是否为特定类型,如果是,则将值 go 替换出循环,再次启动循环->第一个值已被替换,因此索引已更新
它在下一个值之前,直到所有值都被替换

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

我想扩展 function 以便它可以用多行和多列替换嵌套列表

output 应该是这样的

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

问题是如何更新外循环中的rowindex,并用更新后的rowindex开始内循环?

单行列表示例


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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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