簡體   English   中英

將項目的每個子列表添加到Python中的列表列表

[英]Add an Item each sublist to the list of Lists in Python

我是使用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格式,但是有更好的方法嗎?

請提出建議。

我認為以上答案會給您您想要的。 但是,您嘗試過熊貓圖書館嗎? 我認為這將是管理此類數據的好方法。 看這個例子:

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

使用熊貓,您可以根據需要操縱每個列和行,甚至可以輕松地轉換con CSV,EXCl。

您可以在每個子列表的開頭插入項目,如下所示:

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']]

如果需要,可以使用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)

迭代方法是必需的,但是如果您希望代碼更簡潔,則可以使用python中的map()函數,該函數允許您對列表執行按元素操作。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM