Q:

How to cross 2 two-dimensional arrays in python?

I'm trying to make a program that will take two files representing grids and make them overlap (crosswords).
Let me explain this more clearly:
For each cell in my grid, I'm trying to find a char that's both on the row of the horizontal file and on the column of the vertical file.
File "horizontal.txt":
bac
def
hig

File "vertical.txt":
dhc
abf
gei

Program's output:
abc
def
ghi

For cell [0][0] (top left), the letter that is both on row 0 of the horizontal file and on column 0 of the vertical file is "a"
So basically, the words in rows and columns are anagrams and I want to find a way to construct the final table from the first two.
I've tried this in python to find the common letters (my grid is 12x12):
#!/usr/bin/env python

import re

def printCrossword(c):
    for r in range(12):
        print ''.join(c[r])

with open('h.txt') as hFile:
    hFileData = hFile.readlines()

with open('v.txt') as vFile:
    vFileData = vFile.readlines()

hData = [[0 for x in xrange(12)] for x in xrange(12)]
vData = [[0 for x in xrange(12)] for x in xrange(12)]
fData = [[0 for x in xrange(12)] for x in xrange(12)]

for r in range(12):
    for c in range(12):
        hData[r][c] = hFileData[r][c]
        vData[c][r] = vFileData[r][c]

for r in range(12):
    for c in range(12):
        common = re.sub('[^' + ''.join(hData[r]) + ']', '', ''.join(vData[r]))
        if len(common) == 1:
            fData[r][c] = common
        else:
            fData[r][c] = ' '

printCrossword(hData)
print '------------'
printCrossword(vData)
print '------------'
printCrossword(fData)

Here is a graphical representation of the process for the first 4 cells :

A:

I'm trying to find a char that's both on the row of the horizontal file and on the column of the vertical file.

Aside from in- and output, which I won't go into, what you need is a simple intersection algorithm of the character sets in each possible row/column pair. Luckily Python has sets built in (and they support intersection via an overloaded & operator):
# just row- and column-wise traversals of the grid
# I believe these correspond exactly to your hData and vData tables
rows = [ "bac", "def", "hig" ]
cols = [ "dag", "hbe", "cfi" ]

res = [ [ set(r) & set(c) for c in cols ] for r in rows ]
print(res)

This will compute the sets of characters that could appear at the corresponding position in the output (in general, there might be multiple candidates):
[[set(['a']), set(['b']), set(['c'])], 
 [set(['d']), set(['e']), set(['f'])], 
 [set(['g']), set(['h']), set(['i'])]]

In case you know upfront that there is no ambiguity (e.g. if all characters are distinct) and that the grid is solvable, you can use the next function:
res = [ [ next(iter(set(r) & set(c))) for c in cols ] for r in rows ]

Output:
[['a', 'b', 'c'], ['d', 'e', 'f'], ['g', 'h', 'i']]

