2014-09-28 95 views

回答

3
In [99]: A = [ [[1,4.9], [2,90],[3,8]], [[2,34],[4,78],[9,10]], [[1,90],[3,100]] ] 

In [100]: [[item[0] for item in subl] for subl in A] 
Out[100]: [[1, 2, 3], [2, 4, 9], [1, 3]] 
+0

這是一個好問題的答案+1,但它可能是比OP期待的更先進。將它分解爲基本的循環,然後使用列表理解可能會更好。 – agconti 2014-09-28 04:06:16

1
[map(None,*k)[0] for k in A] 
[(1,2,3),(2,4,9),(1,3)] 
0
def getFirstValues(A): 
    B = list() 
    for eachListofList in A: 
     currentList= list() 
     for eachList in eachListofList: 
      currentList.append(eachList[0]) 
     B.append(currentList) 
    return B 
+0

解釋可能會有幫助。 – 2014-09-28 06:42:19

相關問題