First items in inner list efficiently as possible [duplicate]
Solution 1:
This should do it:
c = [x[0] for x in A]
It's a list comprehension that takes the first (sub-)element of every element of A
.
Solution 2:
For efficieny and extended slices, you can use numpy
- which given your example seems like a good idea:
import numpy as np
yourlist = [
[0, 0, 0],
[0, 1, 1],
[1, 0, 2]
]
a = np.array(yourlist)
print a[:,0]
# [0 0 1]
bc = np.bincount(a[:,0])
# array([2, 1])
count = bc[bc==1].size
# 1
# or... (I think it's probably better...)
count = np.count_nonzero(bc == 1)