0

I have 29 text files as follows

File 1

12313 : 23546

        12313

        23214

        32465

File 2

13132 : 23546

        12323

        32125

        32125

        32121

        .

        .

        .

I would like to have python script which computes intersection between these files

ideal result as follow

12313 : 23546

13132 : 23546

Edited by pyTony: indention for fixed text format added, use Code Blocks for fixed text content

3
Contributors
4
Replies
7
Views
5 Years
Discussion Span
Last Post by vegaseat
0

You can use set and & for intersection:

d1 = """
12313 : 23546

        12313

        23214

        32465
"""

d2 = """
13132 : 23546

        12323

        32125

        32125

        32121
"""


ds1 = set(line[8:].strip() for line in d1.splitlines() if line)
ds2 = set(line[8:].strip() for line in d2.splitlines() if line)

print ds1 & ds2

""" Output:
set(['23546'])
"""

Edited by pyTony

0

Hi pyTony,

Thank you very much for reply but i have 29 files i have like that how should i store files and compute intersection between all files

And finally want to print first column ID and common id between files as follows

12313 : 23546

13132 : 23546

Can you please suggest how to do it

Thanks in advance

Ni

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.