Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to merge two maps using python

Fairly new to python so this may seem noobish. I have created two basic maps in .line format (new to me), each showing different features using matplotlib. These maps were converted from separate shapefiles. Now I need to merge these two .line maps into one.

Any ideas would be highly appreciated. TY.

like image 555
kawakawa Avatar asked Jul 25 '26 20:07

kawakawa


2 Answers

In Python 3.5 or greater:

z = {**x, **y}

In Python 2, (or 3.4 or lower) write a function:

def merge_two_dicts(x, y):
    z = x.copy()   # start with x's keys and values
    z.update(y)    # modifies z with y's keys and values & returns None
    return z

z = merge_two_dicts(x, y)
like image 190
Jaganath Kamble Avatar answered Jul 28 '26 08:07

Jaganath Kamble


By 'maps', I assume you mean what are called mappings in Python. The most common example is a dict.

In recent versions of Python (not sure starting when, but more recent than the 3.4 in the other answer), this will work:

z = x | y

The result will be a new mapping, z, which will contain all the elements of x and y, with y winning where there are any duplicates.

like image 29
Michael Scheper Avatar answered Jul 28 '26 08:07

Michael Scheper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!