使用嵌套的while循环代替for循环。

问题描述 投票:0回答:1

我为自己创造了一个编码挑战

我想在不创建新的列表的情况下,使用while循环改变一个嵌套列表的值。

这个函数在单行嵌套列表中效果很好,请看下面的例子。

基本上它的工作原理是这样的:开始循环检查第一个值是否是一个特定的类型,如果是的话,就把valuego从循环中移出,再次开始循环--> 第一个值已经被替换了,所以索引会被更新,并且用下一个值作为前面的值,直到所有的值都被替换。

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

问题是如何在外循环中更新rowindex,然后用更新后的rowindex启动内循环?

单行列表的例子


listoflist = [[None, None, None]]

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

while True:
    insertdata(_execute())
    print(listoflist)
python while-loop nested-lists
1个回答
0
投票

你可以这样做。

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。

© www.soinside.com 2019 - 2024. All rights reserved.