How to compare one element of one array numerically to another element in another array in python
Question: I am new to python and made two arrays for two tab delimited input files.I was
searching for a way to compare the column(element) of one array numerically to
element of another array. Is there any way to do that.My code till now is as
below

    
    
    #!/usr/bin/python
    import sys
    from array import *
    #print len(sys.argv)
    if len(sys.argv) != 4:
     print 'Usage: python scores.py <infile1> <infile2> <outfile>'
     sys.exit(1)
    
    f1 = open ("12877overlappedallvariants.gvf", "r")
    f2 = open ("unmatched.12877overlappedallvariants.gvf", "r")
    f3 = open ("out.txt", "w")
    
    for line in f1.readlines():
     cols = line.split('\t')
     #print cols[5:6]
    
    
    for line in f2.readlines():
     cols1 = line.split('\t')
     #print cols1[5:6]
    

I tried this below section of code but does not work

    
    
    slice1 = cols[5:6]
    slice2 = cols1[5:6]
    new_list = []
    for element in slice1:
     if element in slice2:
      new_list.append(element)
      print new_list  
    

I am trying to see if the element 5 of array1 is equal to, greater than or
less than element 5 of array2. Any help is appreciated. Thanks.


Answer: It's worth noting that these are lists, not arrays. There is a difference.

You are overwriting `cols` every time you loop - meaning you will only get the
last row of data - a list comprehension is in order here to get all the data
you want, or better yet, simply don't store the data at all - do the operation
you want on it.

Your main problem with the comparison is that you are taking list slices,
rather than just taking the element you need. This is overcomplicating what
you are trying to do.

So, as I mentioned in the comments, there are a lot of improvements you can
make to your initial code - mainly the `with` statement and the `csv` module.

So first of all, use the `with` statement to open your files. We use
`csv.reader()` as well, using the `"excel-tab"` dialect as it's a tab-
delimited file, and `csv.QUOTE_NONNUMERIC` to tell it the values are numbers,
so we don't have to convert them from strings later. Note that if only certain
values are numbers, you will either need to quote all non-numeric values to
use this method, or convert those values explicitly and not use it.

    
    
    with open("file1.tsv") as file1, open("file2.tsv") as file2:
        rows = csv.reader(file2, dialect="excel-tab", quoting=csv.QUOTE_NONNUMERIC)
        rows1 = csv.reader(file2, dialect="excel-tab", quoting=csv.QUOTE_NONNUMERIC)
    

To perform your check, simply do something like this (continued on from within
the `with` block above):

    
    
        for cols, cols1 in zip(rows, rows1): #Use itertools.izip() in 2.x for efficiency.
            first = cols[4]
            second = cols1[4]
    
            if first < second:
                ...
            elif first == second:
                ...
            else: #first > second
                ...
    

Note the use of zip to loop over both files at once. We need to loop over the
files as they return a row at a time, each row being a list of data for each
column. You can then do as you please with the data. I have given an example
of comparing the fifth value (note the index `4` \- python is `0` indexed -
that is, the first value is `0`, so the fifth is `4`).

By doing this all as we loop through the files, we ensure we don't have to
create lists and store the data temporarily - which is useful if we end up
working on large files.

If you needed to use that data a lot later, and wanted it as a list, you could
simply make the two `rows` objects lists, by wrapping a `list()` call around
the `csv.reader()`s.

