2016-12-27 5 views
2

の項目を除去します。どのようにそれは次のようになりますので、私は多次元配列内の配列のそれぞれからのデータの最初の2枚を削除することができます:どのように私ができるPythonは、私はこのような多次元配列を有する多次元アレイ

list1 = [] 
list2 = [] 
for row in rows: 
    list1.append(row[0].split(',')) #to put the split list into the top i i.e. [["asdf","bmnl", "123","456,"0","999","1234","3456"]["qwer","tyui","789","657,"122","9","673","1"]] 
for i in list1: 
    for index in len(list1): 
     if index >=2: 
      list2.append(index) #this does not work and causes errors 

:これまでのところ、私はこれを行っている

[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]] 

この固定については行くので、出力は次のようになります。

[["123","456,"0","999","1234","3456"],["789","657,"122","9","673","1"]] 

おかげ

答えて

2

あなたはを使用することができます以下のように:

[item[2:] for item in my_list] 

リストのスライスと呼ばれるitem[2:]、それはmy_listの各サブリストについては、我々は最後の項目までのインデックス2からitemsを取ることを意味します。

出力:

>>> my_list = [["asdf", "bmnl", "123", "456", "0", "999", "1234", "3456"], ["qwer", "tyui", "789", "657", "122", "9", "673", "1"]] 
>>> 
>>> [item[2:] for item in my_list] 
[['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']] 
4

ただ、リストの内包表記を使用して、インデックス2と各サブリスト内を越えてからのすべての要素をつかみます:

>>> array = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]] 
>>> [sublist[2:] for sublist in array] 
[['123', '456', '0', '999', '1234', '3456'], ['789', '657', '122', '9', '673', '1']] 
0
start = [["asdf","bmnl", "123","456","0","999","1234","3456"], 
     ["qwer","tyui","789","657","122","9","673","1"]] 

list_1, list_2 = [i[2:] for i in start] # I use list comprehension to create a multidimensional 
             # list that contains slices of each object in the base 
             # list and unwrap it to list_1 and list_2 

同じ

n_list = [] #create an empty new list 
for i in start: #loop through the origional 
    n_list.append(i[2:]) #append slices of each item to the new list 
list_1, list_2 = n_list #unwrap the list 
+0

として他の人は、それがどのように動作するかを理解できるように、コードと一緒に行くためにいくつかの説明を記載してください – hardillb

3
lst = [["asdf","bmnl", "123","456","0","999","1234","3456"],["qwer","tyui","789","657","122","9","673","1"]] 
for i in lst: 
    del i[0:2] #deleting 0 and 1 index from each list 

print lst