user20480905
user20480905

Reputation:

I there a possibility to limit the values needed, in list comprehension for my function "isSymmetric", which determines if relations are symmetric?

I have a problem in the code for my function isSymmetric, which tests if a relation is symmetric. In the following, I am going to describe my problem further. It is important to mention that I am only allowed to use list comprehension and the any-or all-functions. At first here is my code:

def isSymmetric(base_amount, relation):
    return all([(a,b) in relation and (b,a) in relation for a in base_amount and b in base_amount])


print(isSymmetric([1,2,3], [(1,2),(2,1),(2,2),(1,3),(3,1)]))   #needs to be true
print(isSymmetric([1,2,3], [(1,2),(2,1),(2,2),(1,3)]))         #needs to be false

The problem is that my first output is declared False, even though all the relations are symmetric.

I guess the problem is, that there aren't all possible symmetric relations in the tuples, for example (2,3) and (3,2) aren't included. It has to be true, but I can't come to the conclusion on how to still get True in the first one. I am a beginner and I can't find any helpful material anywhere, on how to really code relations in list comprehension. I would really appreciate a tip or any approach.

Upvotes: 1

Views: 34

Answers (1)

tzaman
tzaman

Reputation: 47850

If you just need to check whether a symmetric relation exists in for every tuple in the list, you don't need the base_amount at all:

def isSymmetric(relation):
    return all((b, a) in relation for (a, b) in relation)

isSymmetric([(1,2),(2,1),(2,2),(1,3),(3,1)])  # True
isSymmetric([(1,2),(2,1),(2,2),(1,3)])        # False

If for some reason you need to construct your pairs from the individual elements, you can do that too by putting a condition in the list-comp, so it only checks the mirror if the forward pair exists:

def isSymmetric(base_amount, relation):
    return all((b, a) in relation for a in base_amount for b in base_amount if (a, b) in relation)

isSymmetric([1,2,3], [(1,2),(2,1),(2,2),(1,3),(3,1)])  # True
isSymmetric([1,2,3], [(1,2),(2,1),(2,2),(1,3)])        # False

Upvotes: 0

Related Questions