【问题标题】:looping over a nested list with multiple rows and columns循环遍历具有多行和多列的嵌套列表
【发布时间】:2020-05-24 14:18:45
【问题描述】:

我想用来自

的输出替换来自 listoflist = [[None, None, None]] 的项目
def _execute():
    user_input = input("type in: ")
    return user_input

不创建新列表

def insertdata(data): 正在这样做。它从第一个值开始,一直持续到最后一个值

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



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

我想扩展该函数以遍历具有多行的嵌套列表。

if 语句应该有什么条件才能从第一行开始执行内部循环`?

def double_loop(data):
    data_added_outer = False
    index_outer = 0
    n_out = len(listoflist)

    # outer loop
    while not data_added_outer and index_outer < n_out:
        if: # what condition ?


            #### inner loop ###############
            data_added_inner = False
            n_inner = len(listoflist[index_outer])
            index_inner = 0
            while not data_added_inner and index_inner != n_inner:
                if listoflist[index_outer][index_inner] is None:
                    listoflist[index_outer][index_inner] = data
                    data_added = True

                else:
                    index_inner += 1

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

    else:
        index_outer += 1
    if index_outer == n_out:
        print("\n The list is full, No more elements will be added \n")


【问题讨论】:

  • 你可以使用嵌套的for循环来简化一切。
  • 但是使用您的方法看到它,您不需要外循环的条件,因为添加“数据”的唯一条件是该项目必须是“无”,并且可以只能从内部循环中完成。

标签: python list loops nested


【解决方案1】:

Python 中的优雅方法不是替换数据,而是使用推导式创建新数据:

def with_nones_replaced(grid, replacement):
    return [
        [
            replacement if value is None else value
            for value in row
        ]
        for row in grid
    ]

【讨论】:

  • 我忘了提到我想替换同一个列表中的数据而不创建新的,这背后有一个原因,在这种情况下并不重要
  • 您可以随时重新分配它,例如old[:] = new。如果您有效率方面的问题,那就另当别论了,更详细的问题。
【解决方案2】:

据我了解,您的函数的工作只是用传递给函数的data 参数替换二维列表中的None 对象。在这种情况下,我发现一种 for-loop 方法更加简洁,并且是 Pythonic。

def changedata(arr, data):

    for item in arr:    
        for x in range(len(item)):

            if item[x] is None:
                item[x] = data  

你也可以使用map函数来简化代码:

def changedata(arr, data):
    for i in range(len(arr)):

        arr[i] = list(map(lambda x: data if x is None else x, arr[i]))

如果数据来自函数,您仍然可以通过调用来使用此范例 _execute() 一次,将其存储在一个变量中,然后将其传递给changedata()

data = _execute() 
changedata(listoflist, data)
print(listoflist)

【讨论】:

  • 我忘了提到传入的数据来自一个等待输入的函数,所以传入的数据会一次插入所有“单元格”,这就是我使用 while 循环的原因跨度>
  • 这是什么意思?请澄清。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多