0

I have a list:

l = [['a', []], ['b', []], ['c', []], ['d', ['c']], ['e', ['d']], ['f', []], ['g', ['f', 'a', 'e']], ['h', ['g']]]

I want to count how many times an element doesn't appear in another list, for example in this list it would be b and h.

These are the only elements that appear only one time in all the list , so the function would return 2.

Another example:

l2 = [['a', []], ['b', []], ['c', ['b']], ['d', ['c', 'b', 'a']], ['f', ['d']], ['g', ['f', 'a']], ['h', ['f', 'c']], ['i', ['h', 'a']]]

Here we got 2 elements that appear only one time in all the lists: g and i

I want to use the python function count but I don't really know how to apply it in this case.

Akshat Zala
  • 710
  • 1
  • 8
  • 23
  • 2
    Does this answer your question? [Counting how many times there are blank lists in a list of list](https://stackoverflow.com/questions/62733105/counting-how-many-times-there-are-blank-lists-in-a-list-of-list) – Umutambyi Gad Jul 04 '20 at 20:25
  • 2
    `have = set([i for s in [x[1] for x in l] for i in s])` and then `print([x[0] for x in l if x[0] not in have])` - posted as a comment because I am not in the habit of posting unintelligible one-liners as answers, but it does actually work. – alani Jul 04 '20 at 20:26
  • 1
    `set(x[0] for x in l) ^ set(y for x in l for y in x[1])` – deadshot Jul 04 '20 at 20:30
  • Does this answer your question? [Counting the occurrences / frequency of array elements](https://stackoverflow.com/questions/5667888/counting-the-occurrences-frequency-of-array-elements) – 10 Rep Jul 04 '20 at 20:36
  • @deadshot my reading of the question (although slightly ambiguous) was such that you might want `-` rather than `^` -- not that it makes any difference in this case. – alani Jul 04 '20 at 20:39
  • Ah, it seems that in any case the question just asks for a count, but that's easily fixed... – alani Jul 04 '20 at 20:43

2 Answers2

1

n contains the elements that appear only once

l = [['a', []], ['b', []], ['c', []], ['d', ['c']], ['e', ['d']], ['f', []], ['g', ['f', 'a', 'e']], ['h', ['g']]]
m = []
n = []
counter = []

for i in l:
    m.append(i[0])

    for j in i[1]:
        m.append(j)

m.sort()

for i in m:
    count = 0
    for j in m:
        if i == j:
            count += 1
    counter.append(count)

n = [m[i] for i in range(0,len(m)) if counter[i] == 1]
qmorgan
  • 85
  • 4
1

Hello based on what you requested I have made a function that I called count(matrix) it returns an array of char that exist once in the matrix provided as a paramter

def count(matrix):
    result=[]
    removed= []
    for line in matrix:
        if line[0] in result :
            result.remove(line[0])
        elif line[0] not in removed:
            result.append(line[0])
            removed.append(line[0])
        for e in line[1]:
            if e in result :
                result.remove(e)
            
            elif e not in removed:
                result.append(e)
                removed.append(e)
    return result

Hope this was useful

qmorgan
  • 85
  • 4
Anass ABEA
  • 479
  • 5
  • 14