Comparing two columns in different file and printing match if difference between record in a columns less than or equal to 0.001

Nerdd

I have two text files, say file1.txt contains something like

100.145 10.0728
100.298 10.04
100.212 10.0286

and file2.txt contains something like

100.223 8.92739
100.209 9.04269
100.084 9.08411

I want to compare column 1 and column 2 of both files and print match if the difference of both columns in file1.txt and in file2.txt is less or equal to 0.001.

weidler

Just read both files, split by line break, split those lines by spaces and then loop over the first files lines and for each line check whether the second files line at this position matches your condition.

with open("file1.txt", "r") as f:
  f1_content = f.read()

with open("file2.txt", "r") as f:
  f2_content = f.read()

f1_lines = [line.split() for line in f1_content.split("\n")]
f2_lines = [line.split() for line in f2_content.split("\n")]

for i, line in enumerate(f1_lines):
  if abs(float(line[0]) - float(f2_lines[i][0])) <= 0.001 and abs(float(line[1]) - float(f2_lines[i][1])) <= 0.001:
    print("Match at line {0}".format(i))

Solution using zip as proposed by @BrianCain:

with open("file1.txt", "r") as f:
  f1_content = f.read()

with open("file2.txt", "r") as f:
  f2_content = f.read()

f1_lines = [line.split() for line in f1_content.split("\n") if line != ""]
f2_lines = [line.split() for line in f2_content.split("\n") if line != ""]

for line in zip(f1_lines, f2_lines):
  if abs(float(line[0][0]) - float(line[1][0])) <= 0.001 and abs(float(line[0][1]) - float(f2_lines[1][1])) <= 0.001:
    print("Match at line {0}".format(line))

Requested Explanation of the solution using zip: First we open both file (using with, as this closes the file after the block) and save the content to some variables. We use read() instead of readlines() bacause the latter doesnt remove an \n when splitting at linebreaks. So i usually just use read() and split by \n, as this does the job.

We then create ourselves for each file a list of tuples. Each entry in the list represents one line, each tuple (which is one of those entries) contains one entry per column. This is done using list comprehensions:

f1_lines = [line.split() for line in f1_content.split("\n") if line != ""]

now we can iterate over the lines. zip is pretty useful for that, as it can combine two lists of equal length to one list, containing tuples of the entries of the lists at the same index. Example:

zip([1, 2, 3], [4, 5, 6])

would produce

[(1, 4), (2, 5), 3, 6)]

For more look here: zip

So now we have a list, containing tuples for the lines with an entry for each file. Each entry itself contains two entries for the columns:

[(['100.145', '10.0728'], ['100.223', '8.92739']),
 (['100.298', '10.04'], ['100.2985', '10.04001']),
 (['100.212', '10.0286'], ['100.084', '9.08411']),
 (['100.212', '100.2125'], ['100.084', '100.0845'])]

if we iterate over this list we can access files via the first [x] as in line[0] and columns via the second [y] as in line0 (accessing the first file at line at the second column). Now we simply subtract at each line the second files first column value from the first files first column value, take the absolute value (to only get positive floats) and check if its less than or equal to 0.001. The same is done for the second column, and if both are true (or one, if you use OR) then we print our message.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Comparing two columns and printing out the difference?

How to remove a row if the difference between two columns is less than 2000

comparing the first column of two files and printing the entire row of the second file if the first columns match

Comparing string differences between two columns in the same row in R, then printing any difference(s) between the two in a new column

Comparing multiple columns of different files and appending a column from a file if there is a match

Excel - conditional formatting between two columns of numbers (greater or less than)

Comparing values between two columns

if match between column ID in two different datasets, then create a new dataset with the difference of other columns r

Dplyr - add column that searches for difference of <30 days between two date columns in different rows if CustomerID is a match

R Match two string columns between two different dataframes

Comparing two columns in two different tables?

Comparing two columns in two different excel sheets

Comparing Columns in different Workbooks, editing rows that match

How to subtract two different values from two different columns and print if less than a value?

Comparing two dataframes of different length row by row and adding columns for each row with equal value

Delete rows based on a match between two different columns in a data frame

Excel formula to pick difference between 2 columns but only if difference is less than 1

Calculating difference between two rows but different columns in Pandas

How to get percentage difference between two columns of different DataFrames?

How to filter comparing two different columns DAX

Comparing data from two different columns in streamlit

Comparing date columns between two dataframes

Running Difference between two columns

Excel Match Values from column and check date in two columns with greater or less than

Oracle: Comparing string columns from two different table without a primary key to find match/unmatched string

Group by using 2 columns and if the time difference between the rows of third column is less than 2 seconds python pandas

Finding Matches between two columns of different files and printing it out a different column

Find equal columns between two dataframes

How to create column with the numbers 0 or 1 when substraction two columns in the same dataframe is less than zero?

TOP Ranking

HotTag

Archive