Python 对矩阵中只出现一次的元素进行计数

Python 对矩阵中只出现一次的元素进行计数,python,list,counter,Python,List,Counter,我有一份清单: l = [['a', []], ['b', []], ['c', []], ['d', ['c']], ['e', ['d']], ['f', []], ['g', ['f', 'a', 'e']], ['h', ['g']]] 我想计算一个元素没有出现在另一个列表中的次数,例如,在这个列表中,它将是b和h 这些是在所有列表中仅出现一次的元素 ,因此函数将返回2 另一个例子: l2 = [['a', []], ['b', []], ['c', ['b']], ['d', ['c

我有一份清单:

l = [['a', []], ['b', []], ['c', []], ['d', ['c']], ['e', ['d']], ['f', []], ['g', ['f', 'a', 'e']], ['h', ['g']]]
我想计算一个元素没有出现在另一个列表中的次数,例如,在这个列表中,它将是b和h

这些是在所有列表中仅出现一次的元素 ,因此函数将返回2

另一个例子:

l2 = [['a', []], ['b', []], ['c', ['b']], ['d', ['c', 'b', 'a']], ['f', ['d']], ['g', ['f', 'a']], ['h', ['f', 'c']], ['i', ['h', 'a']]]
这里我们有两个元素在所有列表中只出现一次:g和i


我想使用python函数count,但我真的不知道如何在这种情况下应用它。

n包含只出现一次的元素

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]

n包含只出现一次的元素

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]

您好根据您的要求,我制作了一个名为count(matrix)的函数,它返回一个char数组,该数组在作为参数提供的矩阵中存在一次

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

希望这是有用的

您好根据您的要求,我制作了一个名为count(matrix)的函数。它返回一个字符数组,该数组作为参数存在于提供的矩阵中

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

希望这有用

这能回答您的问题吗
have=set([i代表s in[x[1]代表x in l]代表i in s])
然后
print([x[0]代表x in l,如果x[0]不在have])
-作为注释发布,因为我不习惯将难以理解的一行作为答案发布,但它确实有效。
set(x[0]代表x in l)^set(y代表x in l代表y in x[1])
这是否回答了您的问题@我对这个问题的理解(虽然有点含糊不清)是你可能想要
-
,而不是
^
——这并不是说在这种情况下有什么区别。这回答了你的问题吗
have=set([i代表s in[x[1]代表x in l]代表i in s])
然后
print([x[0]代表x in l,如果x[0]不在have])
-作为注释发布,因为我不习惯将难以理解的一行作为答案发布,但它确实有效。
set(x[0]代表x in l)^set(y代表x in l代表y in x[1])
这是否回答了您的问题@我对这个问题的理解(虽然有点含糊不清)是这样的:你可能想要
-
,而不是
^
——在这种情况下,这并没有什么区别。