Multiple files comparing using python [closed]

2024/11/16 12:41:50

I have 5 files as follows

F1:

ABL1
ABR
AHSG
AIRE
CCKBR
LRBA
CDC27
CENPA

F2:

AKT1
APC
APP
AR
CCND1
C11ORF2
CCNE1
CST6
CTNNB1
DBI
DEFA1
DNMT1
EEF1A1
EEF1G

F3:

ACTG1
AMPH
ANK3
APBA2
APOA1
ARHGDIA
ATP5J
DST
CA1
CA12
DDR1
CALR
CASP6

F4:

ACVR1
ARL4D
RHOA
RHOG
RHOH
BMPR1B
BMPR2
CDC20
CDK4
CDK6
CHN1

F5:

A1BG
A2M
AAMP
ACTB
ADD1
ALAS1
ALB
APLP1
ASNA1
ATP5B

I tried following code

file1=open("F1.txt","r")
file2=open("F2.txt","r")
file3=open("F3.txt","r")
file4=open("F4.txt","r")
file5=open("F5.txt","r")list1=file1.readlines()
list2=file2.readlines()
list3=file3.readlines()
list4=file4.readlines()
list5=file5.readlines()for line1 in list1:for line2 in list2:for line3 in list3:for line3 in list4:for line4 in list5:if line1.strip() in line2.strip() in line3.strip() in line4.strip() in line5.strip():print line1file3.write(line1)

Now I want to compare all this files and find out common words using python script Can anybody help? Can I use sets?

Answer

It's much easier (I didn't do any error checking when opening the files, though):

filenames = ["F1.txt", "F2.txt", "F3.txt", "F4.txt", "F5.txt"]
files = [open(name) for name in filenames]
sets = [set(line.strip() for line in file) for file in files]
common = set.intersection(*sets)
for file in files: file.close()
https://en.xdnf.cn/q/120327.html

Related Q&A

parsing interactive broker fundamental data

Ive successfully pulled data from IB using the api. It comes in XML format and it looks like this...<TotalRevenues currency="USD"><TotalRevenue asofDate="2017-12-31" report…

How to format HTTP request to discord API?

While this code works, it sends "Bad Request". import socket, ssl token = NzMyMzQ1MTcwNjK2MTR5OEU3.XrzQug.BQzbrckR-THB9eRwZi3Dn08BWrM HOST = "discord.com" PORT = 443 t = POST / HTTP…

Python 3.30 TypeError: object of type int has no len() [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable…

Is it possible to disable negative indexing? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.Want to improve this question? Add details and clarify the problem by editing this post.Closed 7 years ago.Improve…

Google Colab Notebook completely freezes when training a YOLO model

I am following a tutorial to train custom object detection model using YOLO. This is the tutorial and also where I got the Notebook Everything works fine until the training bit which is the last cell. …

Generate 4 columns of data such that each row sum to 100

How do I write a python program that can randomly generate 4 columns of data such that the sum of the numbers of each row is 100?

Nan to Num Python

I have multiple array that for those I calculate a linear regression, but sometimes it gives me 0/0 values which gives me a NaN. I know that to convert an array where there are numbers that are NaN you…

Class constructor able to init with an instance of the same class object

Can python create a class that can be initialised with an instance of the same class object?Ive tried this:class Class(): def __init__(self,**kwargs):print selfself = kwargs.get(obj,self)print selfif …

Python Logical Operators

I am currently learning Python3, but I dont understand Logical Operators. Here is the link: http://pymbook.readthedocs.org/en/py3/operatorsexpressions.html#logical-operators First of all, it seems that…

Partial Pivoting In Pandas SQL Or Spark

Partial Pivoting In Pandas SQL Or Spark Make the Year remain as Rows, and have the States Transpose to columns Take Pecentage value of a Gender Male Race White, InputOutput