Nithin Light
Nithin Light

Reputation: 47

I need to simplify the current output and get an clean output result

I have copied over about 2000 lines of tuples to a list from a txt file. Now i need to each element in the list to the subsequent elements in the rest of the list and i need compare each element only once i.e if i take the first element and compared with every element in the list then i can discard it for the rest of the comparison. I have done that successfully, but now i need help simplifying the output.

Here's is my code for comparison block:

R = [(20, 12, 40, 42, 45), (40, 21, 40, 42, 49),
    (6, 19, 22, 36, 48), (2, 5, 20, 24, 33),
    (8, 12, 24, 28, 44), (3, 15, 29, 30, 37),
    (20, 17, 30, 33, 43), (3, 15, 16, 29, 42),
    (17, 18, 20, 35, 39), (20, 21, 23, 43, 48),
    (14, 24, 30, 40, 45)...]


for lineno1, tup in enumerate(R):
    print("")
    # iterate over the current tuple
    for i, num in enumerate(tup):
        # compare every number in the tuple to the rest of the list
        for lineno2 in range(lineno1+1, len(R)):
            tup2 = R[lineno2]
            if num == tup2[i]:
                print(f"In line: {lineno1+1} {tup} No. '{num} is found in line {lineno2+1} {tup2}.")
                break

This is my output:

line: 1 (20, 12, 40, 42, 45) No. 20 is found in line '7' (20, 17, 30, 33, 43).
line: 1 (20, 12, 40, 42, 45) No. 12 is found in line '5' (8, 12, 24, 28, 44).
line: 1 (20, 12, 40, 42, 45) No. 40 is found in line '2' (40, 21, 40, 42, 49).
line: 1 (20, 12, 40, 42, 45) No. 42 is found in line '2' (40, 21, 40, 42, 49).
line: 1 (20, 12, 40, 42, 45) No. 45 is found in line '11' (14, 24, 30, 40, 45).
    
line: 2 (40, 21, 40, 42, 49) No. 21 is found in line '10' (20, 21, 23, 43, 48).
    
line: 3 (6, 19, 22, 36, 48) No. 48 is found in line '10' (20, 21, 23, 43, 48).
    
line: 4 (2, 5, 20, 24, 33) No. 20 is found in line '9' (17, 18, 20, 35, 39).
    
    
line: 6 (3, 15, 29, 30, 37) No. 3 is found in line '8' (3, 15, 16, 29, 42).
line: 6 (3, 15, 29, 30, 37) No. 15 is found in line '8' (3, 15, 16, 29, 42).
    
line: 7 (20, 17, 30, 33, 43) No. 20 is found in line '10' (20, 21, 23, 43, 48).
line: 7 (20, 17, 30, 33, 43) No. 30 is found in line '11' (14, 24, 30, 40, 45).

As you can see the output can get out of hand if i work with 2000 lines of tuples. I got the desired output that i want, but i need some help clearing up the output to get an neat and clear output. In the above output i get maximum of 5 lines of output for each tuple, which can take so much lines if i am working with 1000's of lines of data. I want to simplify the output to get it in a single line for each tuple.

I want output that looks something like this:

Line 1: (20, 12, 40, 42, 45) (7, 5, 2, 2, 11)  #Right side values are  line numbers of the respective element in the tuple
Line 2: (40, 21, 40, 42, 49) (0, 10, 0, 0, 0)
Line 3: (6, 19, 22, 36, 48) (0, 0, 0, 0, 10)
Line 4: (2, 5, 20, 24, 33) (0, 0, 9, 0, 0)
Line 6: (3, 15, 29, 30, 37) (8, 8, 0, 0, 0)
Line 7: (20, 17, 30, 33, 43) (10, 0, 11, 0, 0)

Upvotes: 2

Views: 73

Answers (3)

Giovanni
Giovanni

Reputation: 212

Actually your code is working pretty well, to obtain the output you require it can helps you to add a list when you insert by default 0 if you don't find anything otherwise the line number in 'human' form (index+1):

R = [(20, 12, 40, 42, 45), (40, 21, 40, 42, 49),
    (6, 19, 22, 36, 48), (2, 5, 20, 24, 33),
    (8, 12, 24, 28, 44), (3, 15, 29, 30, 37),
    (20, 17, 30, 33, 43), (3, 15, 16, 29, 42),
    (17, 18, 20, 35, 39), (20, 21, 23, 43, 48),
    (14, 24, 30, 40, 45)...]


for lineno1, tup in enumerate(R):
    print("")
    # iterate over the current tuple
    founded_indexes = []
    for i, num in enumerate(tup):
        # compare every number in the tuple to the rest of the list
        index = 0
        for lineno2 in range(lineno1+1, len(R)):
            tup2 = R[lineno2]
            if num == tup2[i]:
                index = lineno2 + 1
                break
        founded_indexes.append(index)
    print(f"Line {lineno1} {tup} {found_indexes}")

This should print for each value of your tuple the index of the first array that contains it, 0 otherwise.

Upvotes: 2

shredEngineer
shredEngineer

Reputation: 460

You are almost there, you just have to prepare a result array in the outermost loop and populate it in the innermost loop. Then just output the final result and repeat:

for lineno1, tup in enumerate(R):
    matches = [0] * len(tup)
    # iterate over the current tuple
    for i, num in enumerate(tup):
        # compare every number in the tuple to the rest of the list
        for lineno2 in range(lineno1+1, len(R)):
            tup2 = R[lineno2]
            if num == tup2[i]:
                matches[i] = lineno2+1
                break
print(f"line {lineno1+1}: {tup} {matches}")

This outputs:

line 1: (20, 12, 40, 42, 45) [7, 5, 2, 2, 11]
line 2: (40, 21, 40, 42, 49) [0, 10, 0, 0, 0]
line 3: (6, 19, 22, 36, 48) [0, 0, 0, 0, 10]
line 4: (2, 5, 20, 24, 33) [0, 0, 9, 0, 0]

etc.

Upvotes: 3

azro
azro

Reputation: 54168

You may not print in the final if, bu rather store the value. Use the for..else notation to set a 0 is no break has been used in the for loop, the print only if one at least value as been found (can be done with sum != 0 )

for lineno1, tup in enumerate(R):
    res = []
    for i, num in enumerate(tup):
        for lineno2 in range(lineno1 + 1, len(R)):
            tup2 = R[lineno2]
            if num == tup2[i]:
                res.append(lineno2 + 1)
                break
        else:
            res.append(0)

    if sum(res) != 0:
        print(f"In line: {lineno1 + 1} {tup} {res}.")

CODE DEMO

Upvotes: 3

Related Questions