I have a file with mostly 0 and 1. For each line I would like to compare it against all others (without the redundancy) and count the different combinations. It is slightly more complicated as there are also "N" in the matrix when there is missing data. For example if the two lines are:
line1 = "0 0 1 0 1" line2 = "0 1 1 0 1"
Then I would like to create a two by two table like this:
0 1 0| 2 | 1 | 1| 0 | 2 |
At the moment I am doing something like this:
line1_list = line1.split() line2_list = line2.split()
To get the counts I have tried two options. The first if using a for loop:
tab_dict = defaultdict(int) for i in xrange(0,len(line1_list)): key = line1_list[i]+"-"+line2_list[i] tab_dict[key] += 1
And the second is using a combination of map and zip:
tab_dict = Counter(map(lambda x:x+"-"+x,zip(line1_list,line2_list)))
The first option seems to be the fastest but I was wondering if there is a faster way because the matrix I have is very big and it will take a long time to run. I have tried to look for libraries to do this but have not found anything yet.