Python Forum
Counting Element in Multidimensional List
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Counting Element in Multidimensional List
#1
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...
Reply
#2
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Convert each element of a list to a string for processing tester_V 6 359 Jun-16-2021, 02:11 AM
Last Post: tester_V
  Sume Every 10 element in the list and Divided of Sum quest_ 7 597 Nov-27-2020, 10:58 AM
Last Post: perfringo
  Adding List Element if Second part of the List Elements are the Same quest_ 3 541 Nov-25-2020, 04:33 PM
Last Post: bowlofred
  Choose an element from multidimensional array quest_ 2 453 Nov-25-2020, 12:59 AM
Last Post: quest_
  Removing element from list squall 6 681 Nov-22-2020, 09:34 PM
Last Post: jefsummers
  2d Array adds last element to entire list waiteup 2 421 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  How to get index of minimum element between 3 & 8 in list Mekala 2 719 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Counting Number of Element in a smart way quest 2 429 Nov-09-2020, 10:24 PM
Last Post: quest
  Problem printing last element from a list tester_V 3 579 Oct-30-2020, 04:54 AM
Last Post: tester_V
  Finding an element in a 1d list in a 2d array lionrocker221 0 596 Jun-27-2020, 04:50 PM
Last Post: lionrocker221

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020