【问题标题】:Add an Item each sublist to the list of Lists in Python在 Python 中的 Lists 列表中添加一个 Item 每个子列表
【发布时间】:2017-10-03 21:06:56
【问题描述】:

我是 Python 编程新手,请多多包涵。

我有一个如下列表:

[['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'], 
['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['Test-Node1', 'Running', 'Test', '2231', 'Machine2'], 
['Test-Node3', 'Running', 'Test', '2231', 'machine3'], 
['Test-Node2', 'Running', 'Test', '2231', 'Machine4'], 
['Test-Node5', 'Running', 'Test', '2231', 'machine5']]

我希望在每个子列表中添加一些项目(在开头)。所以它应该如下所示:

[['DOMAIN', 'Application' , 'Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'], 
['UAT' , 'CaseCreation' , 'Test-Node4', 'Running', 'Test', '2231', 'machine1'],
['UAT' , 'CaseCreation' , 'Test-Node1', 'Running', 'Test', '2231', 'Machine2'], 
['UAT' , 'CaseCreation' , 'Test-Node3', 'Running', 'Test', '2231', 'machine3'], 
['UAT' , 'CaseCreation' , 'Test-Node2', 'Running', 'Test', '2231', 'Machine4'], 
['UAT' , 'CaseCreation' , 'Test-Node5', 'Running', 'Test', '2231', 'machine5']]

我会尝试通过 for 循环读取 SubList 并将数据操作到 CSV 中,但是有更好的方法吗?

请提出建议。

【问题讨论】:

    标签: python python-3.x


    【解决方案1】:

    我认为上面的答案会给你你想要的。但是,你试过 pandas 库吗?我认为这将是管理此类数据的好方法。看这个例子:

    import pandas as pd
    
    original_list = [['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
    ['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
    ['Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
    ['Test-Node3', 'Running', 'Test', '2231', 'machine3'],
    ['Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
    ['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
    
    df_original= pd.DataFrame(original_list[1:], columns = original_list[0])  #Convert the list into a DataFrame
    
    new_item = ["UAT","UAT","UAT","UAT","UAT"]  #Create a list with the data, or a series
    new_item2 = ["CaseCreation","CaseCreation","CaseCreation","CaseCreation","CaseCreation"]
    
    df_original.insert(0,"DOMAIN",new_item)  # Then you use insert to add the item wherever you want.
    df_original.insert(1,"Application",new_item2)
    
    print(df_original)
    

    输出:

     DOMAIN   Application        Name   Status AppSpace MgmtPort     Agent
    0    UAT  CaseCreation  Test-Node4  Running     Test     2231  machine1
    1    UAT  CaseCreation  Test-Node1  Running     Test     2231  Machine2
    2    UAT  CaseCreation  Test-Node3  Running     Test     2231  machine3
    3    UAT  CaseCreation  Test-Node2  Running     Test     2231  Machine4
    4    UAT  CaseCreation  Test-Node5  Running     Test     2231  machine5
    

    使用 pandas,您可以根据需要操作每一列和每一行,甚至可以轻松转换 con CSV、EXCl。

    【讨论】:

      【解决方案2】:

      您可以在每个子列表的开头插入项目,如下所示:

      ar = [['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'], 
      ['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
      ['Test-Node1', 'Running', 'Test', '2231', 'Machine2'], 
      ['Test-Node3', 'Running', 'Test', '2231', 'machine3'], 
      ['Test-Node2', 'Running', 'Test', '2231', 'Machine4'], 
      ['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
      
      first_row = ['DOMAIN', 'Application']
      other_row = ['UAT' , 'CaseCreation']
      for i in range(len(ar)):
          if i==0:
              for elem in first_row[::-1]:
                  ar[i].insert(0,elem)
          else:
              for elem in other_row[::-1]:
                  ar[i].insert(0,elem)   
      print(ar)
      

      输出:

      [['DOMAIN', 'Application', 'Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'], ['UAT', 'CaseCreation', 'Test-Node4', 'Running', 'Test', '2231', 'machine1'], ['UAT', 'CaseCreation', 'Test-Node1', 'Running', 'Test', '2231', 'Machine2'], ['UAT', 'CaseCreation', 'Test-Node3', 'Running', 'Test', '2231', 'machine3'], ['UAT', 'CaseCreation', 'Test-Node2', 'Running', 'Test', '2231', 'Machine4'], ['UAT', 'CaseCreation', 'Test-Node5', 'Running', 'Test', '2231', 'machine5']]
      

      【讨论】:

        【解决方案3】:

        如果您愿意,可以使用insert 方法,该方法允许在列表中的任何位置插入:

        list_a=[['Name', 'Status', 'AppSpace', 'MgmtPort', 'Agent'],
        ['Test-Node4', 'Running', 'Test', '2231', 'machine1'],
        ['Test-Node1', 'Running', 'Test', '2231', 'Machine2'],
        ['Test-Node3', 'Running', 'Test', '2231', 'machine3'],
        ['Test-Node2', 'Running', 'Test', '2231', 'Machine4'],
        ['Test-Node5', 'Running', 'Test', '2231', 'machine5']]
        
        
        a=['DOMAIN','Application']
        b=['UAT' ,'CaseCreation']
        
        
        
        for first,second in enumerate(list_a):
            if first==0:
                for item_1 in a:
                        second.insert(0, item_1)
        
        
            else:
                for item in b:
                    second.insert(0,item)
        print(list_a)
        

        【讨论】:

          【解决方案4】:

          迭代方法是需要的,但如果您希望代码更简洁,可以使用 python 中的 map() 函数,它允许您对列表执行元素操作。

          new = list(map( lambda x: ['Name', 'Status'] + x if(x[0] is  'Name') else ['UAT' , 'CaseCreation'] + x, oldlist ) )
          

          【讨论】:

            猜你喜欢
            • 2019-03-29
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2017-11-05
            • 2023-02-25
            • 2021-12-22
            • 2012-12-13
            相关资源
            最近更新 更多