Home > Software design >  Frequency of element in list of lists
Frequency of element in list of lists

Time:11-22

I know this has been asked before, but I can't use a dictionary, a set, enumerate, or any imported packages. I have an adjacency list here:

[[1, 4, 5], [4], [5, 6, 7], [8,9,2],[3, 6, 8],[0,1], [4,5,6]]

My current approach for checking for the number 6 is as follows:

six_counter = 0
    for row_idx in range(len(lst)):
        for character in lst[row_idx]:
            if character == 6:
                six_counter  = 1
    print(f'there are {six_counter} nodes going into node 6')

My question is, how do I do this for each row_idx element? I've tried this:

    count = 0
    for row_idx in range(len(lst)):
        for character in lst[row_idx]:
            if character == row_idx:
                count  = 1
        print(f'there are {count} nodes going into {row_idx}')

But the output it gives me is:

there are 0 nodes going into 0
there are 0 nodes going into 1
there are 0 nodes going into 2
there are 0 nodes going into 3
there are 0 nodes going into 4
there are 0 nodes going into 5
there are 1 nodes going into 6
there are 2 nodes going into 7
there are 3 nodes going into 8

Whereas the expected output was:

there are 1 nodes going into 0
there are 2 nodes going into 1
there are 1 nodes going into 2
there are 3 nodes going into 3
there are 3 nodes going into 4
there are 2 nodes going into 5
there are 2 nodes going into 6
there are 1 nodes going into 7
there are 2 nodes going into 8

Thoughts? Ideas that fit within my constraints? Cheers

CodePudding user response:

If you want to count the occurrences of each number without using any imported packages or dictionaries, you can do something like this:

lst = [[1, 4, 5], [4], [5, 6, 7], [8,9,2],[3, 6, 8],[0,1], [4,5,6]]

def find_max_node(list_of_lists):
    max_node = -1
    for list_ in list_of_lists:
        tmp_max_node = max(list_)
        max_node = max(tmp_max_node,max_node)
    return max_node

if __name__ == "__main__":
    max_node = find_max_node(lst)
    count_nodes = list()
    for _ in range(max_node 1):
        count_nodes.append(0)

    for row_idx in range(len(lst)):
        for character in lst[row_idx]:
            count_nodes[character]  = 1

    for i in range(len(count_nodes)):
        print(f'there are {count_nodes[i]} nodes going into {i}')

The result:

there are 1 nodes going into 0
there are 2 nodes going into 1
there are 1 nodes going into 2
there are 1 nodes going into 3
there are 3 nodes going into 4
there are 3 nodes going into 5
there are 3 nodes going into 6
there are 1 nodes going into 7
there are 2 nodes going into 8
there are 1 nodes going into 9

CodePudding user response:

I don't see how your expected answer corresponds to your description. If your objective is to count how many links in the adjacency matrix target each index (including the ones beyond the size of the list), you can use a list to store counters, go through each sublist and add to the counter corresponding to the target index:

adj = [[1, 4, 5], [4], [5, 6, 7], [8,9,2],[3, 6, 8],[0,1], [4,5,6]]

counts = []
for links in adj:                              # run though sublists
    for t in links:                            # run through targets
        while len(counts)<=t: counts.append(0) # add counters as needed
        counts[t]  = 1                         # tally link targets 

for i in range(len(counts)):
    print(f'There are {counts[i]} nodes going into {i}')

There are 1 nodes going into 0
There are 2 nodes going into 1
There are 1 nodes going into 2
There are 1 nodes going into 3
There are 3 nodes going into 4
There are 3 nodes going into 5
There are 3 nodes going into 6
There are 1 nodes going into 7
There are 2 nodes going into 8
There are 1 nodes going into 9
  • Related