Python Forum
Counting Element in Multidimensional List - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Counting Element in Multidimensional List (/thread-31163.html)



Counting Element in Multidimensional List - quest_ - Nov-25-2020

Hello,

I have list like that:

Output:
newlist22 [[[0, 1, 1], [0, 1, 1], [1, 0, 1], [0, 0, 0], [1, 1, 1], [0, 1, 1], [1, 0, 1], (0, 1.57, 0)], [[1, 0, 0], [0, 1, 0], [1, 1, 1], [1, 1, 0], [1, 0, 0], [1, 1, 1], [0, 1, 1], (1.57, 0, 1.57)], [[0, 1, 1], [1, 1, 0], [1, 0, 1], [0, 1, 1], [1, 0, 1], [0, 1, 1], [0, 0, 1], [0, 1, 1], (1.57, 0, 0)], [[0, 1, 1], [0, 1, 0], [0, 1, 0], [0, 0, 1], [0, 0, 0], [0, 1, 0], [0, 1, 0], (0, 1.57, 1.57)], [[1, 0, 1], [0, 0, 0], [1, 1, 0], [0, 1, 0], [0, 0, 0], [1, 1, 1], [0, 0, 0], [0, 0, 0], (0, 0, 0)], [[0, 1, 0], [1, 0, 1], [1, 0, 1], [0, 0, 0], [0, 1, 1], [0, 0, 0], [1, 1, 0], (1.57, 1.57, 1.57)], [[0, 0, 0], [0, 1, 1], [0, 1, 0], [0, 0, 0], [0, 0, 0], [1, 1, 0], [0, 1, 1], [1, 1, 0],(0, 0, 1.57)], [[0, 1, 0], [1, 1, 1], [0, 0, 0], (1.57, 1.57, 0)]]
Now I want to count
First how many [000], [001] ,[010],[011]…(all triplet combinations of 0 and 1) according to last element of the array (last element are (1.57, 0, 1.57),(0, 1.57, 0),(1.57, 0, 0)…)
For instance in the first sublist I have 3 times [0, 1, 1] 1 times [111], 1 times [000] and 1 times [101] and for the forst sublist I have 7 elements except (0, 1.57, 0)
Second, I want to count how many element there are of each subarray except last element (last elements means that element is inside ()) For instance in the first sublist I have 7 element except (0, 1.57, 0)
I can count somethong woth the following line but it counts total [000], [001] ,[010],[011]…
I want to count sublist by sublist
triplets = tuple(itertools.product((0, 1), repeat = 3)) 
for triple in triplets:
    sums[triple] = sum(np.all((newlist22-np.array(triple))==0, axis=1)) 
How can I solve this problem
Thanks for helps...


RE: Counting Element in Multidimensional List - quest_ - Nov-25-2020

Ok I found :)

triplets = tuple(itertools.product((0, 1), repeat = 3)) 
sums = {}
for row in newlist2:
    for triple in triplets:
        sums[triple] = sum(np.all((row-np.array(triple))==0, axis=1))
    print(sums)