Skip to content
Advertisement

comparing a number of lists in pairs

I have problem with 4 list each one got 4 number:

P1= [3,1,3,4]

P2= [5,4,3,7]

P3= [7,4,8,1]

P4= [10,3,2,1]

I need to get the biggest number in combinational way like: Pair of 2: p1p2, p1p3, p1p4, p2p3, p2p4, p3p4

Pairs of 3: p1p2p3, p1p2p4, p1p3p4, p2p3p4

Pairs of 4: p1p2p3p4

The result for:

p1p2= [5,4,3,7]

p1p3= [7,4,8,4]

p1p4= [10,3,3,4] ……

I wrote this program and I store it in a matrix instead of a list. ‘ n= int (input(“Enter the matrix size:”))

matrix=[]
for i in range(n): #loop for rows
    c=[]
    for j in range(n):# loop for columns
        j=int(input("enter the value of P["+str(i)+"]"+"["+str(j)+"]"))
        c.append(j)

    print()                     # to make a line between the entries
    matrix.append(c)            # store the matrix c in the new empty one (matrix)

for i in range(n):
    for j in range(n):
        print(matrix[i][j], end="")
    print()
##############################################
#compar row by row element wise 2 terms
m2t=[]
for i in range(n-1):
    for j in range(n):
        if(matrix[i][j]>=matrix[i+1][j]):
            m2t.append(matrix[i][j])
        else:
            m2t.append(matrix[i+1][j])
            print(m2t,"#1")       # check if it get here or not
    
    print()
#printing the matrix of the 2 terms result 
for a in range(n-1):
    for b in range(n):
        print(m2t, end="")
    print()   

Is there a better way to do it?

Advertisement

Answer

There is at least one more way. Wait for more elegant solutions.

from itertools import combinations
P1= [3,1,3,4]
P2= [5,4,3,7]
P3= [7,4,8,1]
P4= [10,3,2,1]

Full_List = [P1, P2, P3, P4]

for r in range(2,5,1):
    Combinations = combinations(Full_List, r)
    print("Finding maximum for a combination size of %%d"%%r)
    for eachCombination in Combinations:
        print("Finding combination for tuples ", *eachCombination)
        print(list(map(max,zip(*eachCombination))))
        print("Done")
        print()
    print()

Gives the following output.

Finding maximum for a combination size of 2
Finding combination for tuples  [3, 1, 3, 4] [5, 4, 3, 7]
[5, 4, 3, 7]
Done

Finding combination for tuples  [3, 1, 3, 4] [7, 4, 8, 1]
[7, 4, 8, 4]
Done

Finding combination for tuples  [3, 1, 3, 4] [10, 3, 2, 1]
[10, 3, 3, 4]
Done

Finding combination for tuples  [5, 4, 3, 7] [7, 4, 8, 1]
[7, 4, 8, 7]
Done

Finding combination for tuples  [5, 4, 3, 7] [10, 3, 2, 1]
[10, 4, 3, 7]
Done

Finding combination for tuples  [7, 4, 8, 1] [10, 3, 2, 1]
[10, 4, 8, 1]
Done


Finding maximum for a combination size of 3
Finding combination for tuples  [3, 1, 3, 4] [5, 4, 3, 7] [7, 4, 8, 1]
[7, 4, 8, 7]
Done

Finding combination for tuples  [3, 1, 3, 4] [5, 4, 3, 7] [10, 3, 2, 1]
[10, 4, 3, 7]
Done

Finding combination for tuples  [3, 1, 3, 4] [7, 4, 8, 1] [10, 3, 2, 1]
[10, 4, 8, 4]
Done

Finding combination for tuples  [5, 4, 3, 7] [7, 4, 8, 1] [10, 3, 2, 1]
[10, 4, 8, 7]
Done


Finding maximum for a combination size of 4
Finding combination for tuples  [3, 1, 3, 4] [5, 4, 3, 7] [7, 4, 8, 1] [10, 3, 2, 1]
[10, 4, 8, 7]
Done

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement