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

时间:2020-05-26 08:51:20

标签: python while-loop nested-lists

我为我提出了编码挑战

我想更改嵌套列表的值 使用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

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

单行列表示例


listoflist = [[None, None, None]]

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

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

1 个答案:

答案 0 :(得分: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']]

您遇到了索引问题,并且在将新元素添加到列表之后没有花费片刻

相关问题