【问题标题】:Populate list of list with conditions使用条件填充列表列表
【发布时间】:2026-01-26 11:05:01
【问题描述】:

我有一个列表1listoflist = [[],[],[],[],[]] 并希望使用来自此函数的数据填充它:

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

我要做的是将输入函数中的数据插入第一列,例如:

input = 1
[[1],[],[],[],[]]
input = 2
[[1,2],[],[],[],[]]

如果第一列长度大于2,则切换到下一列直到最后一列。

到目前为止,我设法将数据插入到第一列,但它在第一列中插入了两次来自输入函数的返回值:

def insertdata(data):
    for i in range(len(listoflist)):
        if len(listoflist[0]) < 2:
            listoflist[0].append(data)

        else:
            print(listoflist)
            break


insertdata(_execute())



# output
type in: 1
[['1', '1'], [], [], [], []]


我需要用索引来做这个操作吗?

1:更像是对列表,因为内部列表只能存储两个值。

【问题讨论】:

    标签: python list loops iteration


    【解决方案1】:

    您可以将while True 放在_execute() 函数之外(每次返回时,都会绕过while)并保留一个标志来判断您的数据是否已添加,此外,请检查边界以避免索引错误:

    listoflist = [[], [], [], [], []]
    
    def _execute():
        user_input = input("type in: ")
        return user_input
    
    def insertdata(data):
        data_added = False
        i, n = 0, len(listoflist)
        while not data_added and i < n:
            if len(listoflist[i]) < 2:
                listoflist[i].append(data)
                data_added = True
            else:
                i += 1
        if i == n:
            print("\n The list is full, No more elements will be added \n")
    
    while True:
        insertdata(_execute())
        print(listoflist)
    
    

    【讨论】:

      【解决方案2】:

      您只需要在列表列表中找到第一个有空格的列表来处理用户输入。添加用户输入后,您可以返回 - 没有其他事情可做。你得到了双重加法,因为append() 在一个for循环中

      您还应该处理无法添加更多元素的情况:

      listoflist = [[],[],[],[],[]]
      
      def _execute():
          while True:
              user_input = input("type in: ")
              return user_input
      
      def insertdata(data):
          for l in listoflist:
              if len(l) < 2:
                  l.append(data)
                  return
          else:
              print("no more space")
      
      
      while True:
          insertdata(_execute())
          print(listoflist)
      

      输出:

      type in: 1
      [['1'], [], [], [], []]
      type in: 1
      [['1', '1'], [], [], [], []]
      type in: 1
      [['1', '1'], ['1'], [], [], []]
      type in: 1
      [['1', '1'], ['1', '1'], [], [], []]
      type in: 1
      [['1', '1'], ['1', '1'], ['1'], [], []]
      type in: 1
      [['1', '1'], ['1', '1'], ['1', '1'], [], []]
      type in: 1
      [['1', '1'], ['1', '1'], ['1', '1'], ['1'], []]
      type in: 1
      [['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], []]
      type in: 1
      [['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], ['1']]
      type in: 1
      [['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], ['1', '1']]
      type in: 1
      no more space
      [['1', '1'], ['1', '1'], ['1', '1'], ['1', '1'], ['1', '1']]
      

      【讨论】:

        【解决方案3】:

        也许它的存在是出于其他原因,但您的 _execute() 中的 while True 不需要,因为 input 正在阻塞。 此外,我刚刚评论了您的代码块正在做什么

        def insertdata(data):
            for i in range(len(listoflist)): #execute the following code for the amount of lists in listoflist, namely 5 times
                if len(listoflist[0]) < 2: #if you have less than 2 elements in the first element of listoflist
                    listoflist[0].append(data) # append data to the first element
        
                else:
                    print(listoflist)
                    break
        

        有关正确的功能,请参阅 RMPR 的答案。

        【讨论】: