Home > Mobile >  comparing two sets and finding equation about them python
comparing two sets and finding equation about them python

Time:11-29

I have two sets

list1 = {1,2,3,4,5,6,7,8,9,10}
list2 = {10,20,30,40,50,60,70,80,90,100}

I want python to see if there is a relation between each number and if the relation is the same for each number(for this example it would be the same for each number and the relation is *10)or if it is not it would print that they do not have a relation

CodePudding user response:

If you use sets, you cannot define a 1-to-1 relationship, as those are unordered.

If you have lists, you could use:

list1 = [1,2,3,4,5,6,7,8,9,10]
list2 = [10,20,30,40,50,60,70,80,90,100]

ratios = [b/a for a,b in zip(list1, list2)]

Output: [10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0]

Or using a set comprehension:

S = {round(b/a, 3) for a,b in zip(list1, list2)}
# {10.0}

# check there is only one possibility
if len(S) != 1:
    print('there is not a unique ratio')
  • Related