0

I have Just opted the below code which is working fine to determine the duplicate line and line numbers and this works fine.

from collections import defaultdict
def find_dupl(log):
    output = defaultdict(list)
    with open(log) as f:
        for i, line in enumerate(f):
            line = line.strip()
            output[line].append(i)
        print({k: v for k, v in output.items() if len(v) > 1})
find_dupl('rhel65')

The result output is as follows, i every host name or item to be printed into new line.

{'hostlab01': [0, 1], 'hostlab02': [34, 35, 36]}

Desired would be :

hostlab01: [0, 1] 
hostlab02: [34, 35, 36]
Karn Kumar
  • 8,518
  • 3
  • 27
  • 53

3 Answers3

1

Change the output of your function by removing the print statement and defining instead a generator:

def find_dupl(log):
    output = defaultdict(list)
    with open(log) as f:
        for i, line in enumerate(f):
            line = line.strip()
            output[line].append(i)
   result = {k: v for k, v in output.items() if len(v) > 1}
   for k, v in result.items():
       yield k, v

for key, value in find_dupl('rhel65'):
    print(f"{key}: {value}")

That should give you the expected result if I am right.

cestMoiBaliBalo
  • 146
  • 2
  • 5
1

Change one line in your function:

print(*['{}: {}'.format(k, v) for k, v in output.items() if len(v) > 1], sep='\n')
  • @pygo Asterisk `*` here means unpacking of the list and passing its content to the function print as separate variables. https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters –  Sep 14 '18 at 14:10
1

change your print line to:

print("\n".join("{}\t{}".format(k, v) for k, v in output.items() if len(v) > 1))
VanTan
  • 617
  • 4
  • 12