-1

I'm having a little trouble with something I thought should be quite easy.

I have two files, both tsv. One is a list of names like so:

Thing1
Thing2
Thing3
Thing4
...

The other is a list of interactions between these 'things', it looks like so:

Thing1 Thing2 0.726
Thing3 Thing2 0.742
Thing1 Thing4 0.761

All I'd like to do is take the list and return the names of items on that list, which appear in my second interaction file. I thought this should be fairly straight forward but I can't seem to get it working.

So far I have tried a few things but the most basic looks like so:

import csv
import sys

ints = sys.argv[1]

name_list = open('Names', 'rb')
int_list = open(ints, 'rb')

for i in int_list:
    for names in name_list:
        if i == name:
            print(name) 

I'm sure I'm missing something very basic here but any help would be much appreciated.

Cheers :)

M. Herbert
  • 41
  • 7
  • Have you tried printing the variables inside the loop, to see what `row` is? (It is not `Thing1`...) – leo May 11 '16 at 10:47
  • Also: You are importing the CSV lib at top. You might want to consider using it, as that will be much easier for you. – leo May 11 '16 at 10:49
  • Possible duplicate of [Loop for Parsing complex tab delimited/csv files in Python](http://stackoverflow.com/questions/20108308/loop-for-parsing-complex-tab-delimited-csv-files-in-python) – leo May 11 '16 at 10:51
  • You've imported the csv module, but you're not using to actually parse your CSV file. – Daniel Roseman May 11 '16 at 10:53

2 Answers2

0

You must split your names into 3 parts (the first name of file, the interact file, and the value) like this:

for i in int_list:
    for names in name_list:
       name = names.split(' ')

after this split, you could use name[0] to test the correspondence.

zondo
  • 19,901
  • 8
  • 44
  • 83
marcS
  • 96
  • 1
  • 4
0

Pandas' dataframes might help you a lot with that.

import pandas as pd

name_list = pd.read_csv('Names',sep = '\t')
int_list = pd.read_csv('ints',sep = '\t')

int_list[int_list.isin(name_list)]
ysearka
  • 3,805
  • 5
  • 20
  • 41