2017-10-03 12 views
0

私は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ループを介してサブリストを読んでみましたし、CSV何かにデータを操作しますが、良い方法はありますでしょうか?

ご提案ください。

答えて

1

あなたは以下のように各サブリストの先頭に項目を挿入することができます。

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

反復的なアプローチが必要なものですが、あなたのコードを望んでいた場合は、より簡潔にするためにPythonのmap()関数を使うことができます。この関数を使うと、リストに要素ごとの操作を行うことができます。

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

あなたは、リスト内の任意の位置に挿入することができ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) 
2

を私は上記の答えは何をしたいあなたを与えるだろうと思います。しかし、あなたはパンダライブラリーを試しましたか?この種のデータを管理するのがいい方法だと思います。この例を見て:

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 

あなたが望むようあなたも簡単な方法で詐欺CSV、EXCLを変換し、各列と行を操作することができます。

関連する問題