4 examples of 'merge two arrays in python' in Python

Every line of 'merge two arrays in python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
290def _merge_python_type(A, B):
291 a = _merge_order[A]
292 b = _merge_order[B]
293
294 if a >= b:
295 output = A
296 else:
297 output = B
298
299 if isinstance(output, str):
300 return output
301 else:
302 return output.__name__
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

24def merge(a, b):
25 for key in b:
26 if key in a:
27 if isinstance(a[key], dict) and isinstance(b[key], dict):
28 merge(a[key], b[key])
29 else:
30 a[key] = b[key]
31 return a
8def data_merge(obj_a, obj_b):
9 """merges obj_b into obj_a and return merged result
10
11 Args:
12 obj_a (dict) The "merge into dict"
13 obj_b (dict) The "merge from dict"
14
15 Returns:
16 dict: The obj_a with obj_b added to it
17 """
18 key = None
19 try:
20 if obj_a is None or isinstance(obj_a, (str, unicode, int, long, float)):
21 # border case for first run or if a is a primitive
22 obj_a = obj_b
23 elif isinstance(obj_a, list):
24 # lists can be only appended
25 if isinstance(obj_b, list):
26 # merge lists
27 obj_a.extend(obj_b)
28 else:
29 # append to list
30 obj_a.append(obj_b)
31 elif isinstance(obj_a, dict):
32 # dicts must be merged
33 if isinstance(obj_b, dict):
34 for key in obj_b:
35 if key in obj_a:
36 obj_a[key] = data_merge(obj_a[key], obj_b[key])
37 else:
38 obj_a[key] = obj_b[key]
39 else:
40 raise 'Cannot merge non-dict "%s" into dict "%s"' % (obj_b, obj_a)
41 else:
42 raise 'NOT IMPLEMENTED "%s" into "%s"' % (obj_b, obj_a)
43 except TypeError, exc:
44 raise 'TypeError "%s" in key "%s" when merging "%s" into "%s"' % (exc, key, obj_b, obj_a)
45 return obj_a
130def _merge_a_into_b(a, b, stack=None):
131 """Merge config dictionary a into config dictionary b, clobbering the
132 options in b whenever they are also specified in a.
133 """
134
135 assert isinstance(a, AttributeDict) or isinstance(a, dict), 'Argument `a` must be an AttrDict'
136 assert isinstance(b, AttributeDict) or isinstance(a, dict), 'Argument `b` must be an AttrDict'
137
138 for k, v_ in a.items():
139 full_key = '.'.join(stack) + '.' + k if stack is not None else k
140 # a must specify keys that are in b
141 if k not in b:
142 # if is it more than second stack
143 if stack is not None:
144 b[k] = v_
145 else:
146 raise KeyError('Non-existent config key: {}'.format(full_key))
147
148 v = copy.deepcopy(v_)
149 v = _decode_cfg_value(v)
150
151 v = _check_and_coerce_cfg_value_type(v, b[k], k, full_key)
152
153 # Recursively merge dicts
154
155 b[k] = v

Related snippets