Concatenate Two Single-dimension List To Get One Multi-dimensional List
For my application scope, I need to concatenate two one-dimension array into one multi-dimension array, both implemented using (eventually nested)
lists in Python. The concatenations have to print all the possible combinations between the elements of the first array with the elements of the second array.
The expected result is a multi-dimension array with the combinations of
vectA with all the elements of
vectB (cartesian product).
No need to import a package here.
You can do this with simple list comprehensions, too:
vectA = [124, 172, 222, 272, 323, 376, 426, 479, 531] vectB = [440, 388, 336, 289, 243, 197, 156, 113, 74] output = [[a, b] for a in vectA for b in vectB] print(output)
Also, I would propose to output a list of tuples instead of a list of lists:
output = [(a, b) for a in vectA for b in vectB]
[(124, 440), (124, 388), (124, 336), ... , (531, 74)]
Using tuples would, in my opinion, more clearly convey to someone else your intention of pairing all the values of vectA with all the values of vectB.
You can still do e.g.
output to get
(124, 440) and
output to get
124 as you would with a list of lists.
Note though, that you can not overwrite the values of a tuple like you could with values of a list, since tuples are immutable.
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module