Reputation: 73
l have the following input list composed of values which varies from k=0 to 4:
vector_input= [(3, 3, 3, 3, 3), (0, 0, 1), (3, 4, 3, 3, 4, 4), (1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 3, 3, 3, 1, 1, 3), (0, 0), (4, 4, 4, 4, 4), (1, 1), (3, 3), (1, 4, 3, 2), (3, 3, 4, 4, 4), (4, 4, 4, 4, 4), (3,), (2, 2), (2, 2, 2, 2, 2, 2), (0, 0, 0, 0, 1, 0), (1, 1, 1, 1, 1), (1, 1, 1, 1, 1), (0, 0, 0, 0, 0, 0, 1, 0, 0, 1)]
l want to transform this list into a list of frequency output of dimension of k (k=4) so that to get list output as follows :
vector_output=[
[0,0,0,5,0],[2,1,0,0,0],[0,0,0,3,3],[0,4,0,0,0],[0,16,0,0,0], [0,3,0,4,0],
[2,0,0,0,0],[0,0,0,0,5],[0,0,0,3,0],[0,0,2,0,0],[0,0,6,0,0],[5,1,0,0,0],[0,5,0,0,0],[0,5,0,0,0],[8,2,0,0,0]]
For instance : (3, 3, 3, 3, 3) becomes (0,0,0,5,0) because three is repeated five times and 0,1,2,4 zero times.
Upvotes: 0
Views: 49
Reputation: 54223
You could create a distribution function:
def distribution(vector, highest=5):
dist = [0] * highest
for i in vector:
dist[i] += 1
return dist
and apply it to every vector with a list comprehension:
vector_input= [(3, 3, 3, 3, 3), (0, 0, 1), (3, 4, 3, 3, 4, 4), (1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 3, 3, 3, 1, 1, 3), (0, 0), (4, 4, 4, 4, 4), (1, 1), (3, 3), (1, 4, 3, 2), (3, 3, 4, 4, 4), (4, 4, 4, 4, 4), (3,), (2, 2), (2, 2, 2, 2, 2, 2), (0, 0, 0, 0, 1, 0), (1, 1, 1, 1, 1), (1, 1, 1, 1, 1), (0, 0, 0, 0, 0, 0, 1, 0, 0, 1)]
print([distribution(v) for v in vector_input])
# [[0, 0, 0, 5, 0], [2, 1, 0, 0, 0], [0, 0, 0, 3, 3], [0, 4, 0, 0, 0], [0, 16, 0, 0, 0], [0, 3, 0, 4, 0], [2, 0, 0, 0, 0], [0, 0, 0, 0, 5], [0, 2, 0, 0, 0], [0, 0, 0, 2, 0], [0, 1, 1, 1, 1], [0, 0, 0, 2, 3], [0, 0, 0, 0, 5], [0, 0, 0, 1, 0], [0, 0, 2, 0, 0], [0, 0, 6, 0, 0], [5, 1, 0, 0, 0], [0, 5, 0, 0, 0], [0, 5, 0, 0, 0], [8, 2, 0, 0, 0]]
Upvotes: 1
Reputation: 71451
You can try this:
import itertools
vector_input= [(3, 3, 3, 3, 3), (0, 0, 1), (3, 4, 3, 3, 4, 4), (1, 1, 1, 1), (1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), (1, 3, 3, 3, 1, 1, 3), (0, 0), (4, 4, 4, 4, 4), (1, 1), (3, 3), (1, 4, 3, 2), (3, 3, 4, 4, 4), (4, 4, 4, 4, 4), (3,), (2, 2), (2, 2, 2, 2, 2, 2), (0, 0, 0, 0, 1, 0), (1, 1, 1, 1, 1), (1, 1, 1, 1, 1), (0, 0, 0, 0, 0, 0, 1, 0, 0, 1)]
full_listing = list(set(itertools.chain(*vector_input)))
final_data = [tuple(list(b).count(c) for c in range(min(full_listing), max(full_listing)+1)) for b in vector_input]
Output:
[(0, 0, 0, 5, 0), (2, 1, 0, 0, 0), (0, 0, 0, 3, 3), (0, 4, 0, 0, 0), (0, 16, 0, 0, 0), (0, 3, 0, 4, 0), (2, 0, 0, 0, 0), (0, 0, 0, 0, 5), (0, 2, 0, 0, 0), (0, 0, 0, 2, 0), (0, 1, 1, 1, 1), (0, 0, 0, 2, 3), (0, 0, 0, 0, 5), (0, 0, 0, 1, 0), (0, 0, 2, 0, 0), (0, 0, 6, 0, 0), (5, 1, 0, 0, 0), (0, 5, 0, 0, 0), (0, 5, 0, 0, 0), (8, 2, 0, 0, 0)]
Upvotes: 0