Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python "join" function like unix "join"

Tags:

python

join

I am curious if there is a built-in python join function like the unix version (see http://linux.about.com/library/cmd/blcmdl_join.htm https://www.man7.org/linux/man-pages/man1/join.1.html). I know the functionality is included through the built in sqlite3 module and probably through some other modules like pytables.

Sorry if this is a basic question, but I'm finding that searching for "python join" and related queries is fairly polluted by the standard python join function. Also, if there is no such functionality, I would not expect to find that information so easily.

like image 826
mathtick Avatar asked Dec 22 '22 21:12

mathtick


2 Answers

Here's a python version of the join function, does not handle all of the potential error cases. But demonstrates the basic idea.

# usage join(open('f1.txt'), open('f2.txt'))

def join(fd_a, fd_b) :
    result = []
    la = fd_a.readline()
    lb = fd_b.readline()
    while la and lb :
        start_a, rest_a = la.split(' ', 1)
        start_b, rest_b = lb.split(' ', 1)
        if cmp(start_a, start_b) == 0 :
            result.append([start_a, [rest_a, rest_b]])
            la = fd_a.readline()
            lb = fd_b.readline()
        elif cmp(start_a, start_b) < 0 :
            la = fd_a.readline()
        else :
            lb = fd_b.readline()
    return result
like image 128
koblas Avatar answered Dec 24 '22 11:12

koblas


You can easily simulate a join using dictionaries:

d1 = {"a": 1, "b": 2}
d2 = {"b": 3, "c": 4}
print dict((key, (d1[key], d2[key])) for key in d1 if key in d2)
{'b': (2, 3)}

Or, the last line in Python 3.x:

print {key: (d1[key], d2[key]) for key in d1.keys() & d2.keys()}
{'b': (2, 3)}

(The 2.x line also works in Python 3.x, but the use of & to get the intersection of the dictionary keys may speed things up if only a small percentage of the keys of d1 are in the intersection.)

I don't know of a built-in to do this in a single function call.

like image 36
Sven Marnach Avatar answered Dec 24 '22 09:12

Sven Marnach