Categories
Python Answers

How to use numpy to build an array of all combinations of two arrays with Python?

Sometimes, we want to use numpy to build an array of all combinations of two arrays with Python.

In this article, we’ll look at how to use numpy to build an array of all combinations of two arrays with Python.

How to use numpy to build an array of all combinations of two arrays with Python?

To use numpy to build an array of all combinations of two arrays with Python, we can use the numpy.meshgrid method.

For instance, we write

np.stack(np.meshgrid([1, 2, 3], [4, 5], [6, 7]), -1).reshape(-1, 3)

to call meshgrid with a nested list to return the cartesian product of the nested lists.

Then we reshape the returned array with reshape into a nested list with 3 items in each list and transposed.

Conclusion

To use numpy to build an array of all combinations of two arrays with Python, we can use the numpy.meshgrid method.

Categories
Python Answers

How to pretty print XML in Python?

Sometimes, we want to pretty print XML in Python.

In this article, we’ll look at how to pretty print XML in Python.

How to pretty print XML in Python?

To pretty print XML in Python, we can use the xml.dom.minidom module’s toprettyxml method.

For instance, we write

import xml.dom.minidom

dom = xml.dom.minidom.parse(xml_fname)
pretty_xml_as_string = dom.toprettyxml()

to parse the XML file at path xml_fname with xml.dom.minidom.parse into a DOM object.

Then we call toprettyxml on the DOM object to return the XML as a pretty printed string.

We can also parse XML from a string with

xml.dom.minidom.parseString(xml_string)

where xml_string is an XML string.

Conclusion

To pretty print XML in Python, we can use the xml.dom.minidom module’s toprettyxml method.

Categories
Python Answers

How to shuffle a list of objects with Python?

Sometimes, we want to shuffle a list of objects with Python

In this article, we’ll look at how to shuffle a list of objects with Python.

How to shuffle a list of objects with Python?

To shuffle a list of objects with Python, we can use the shuffle function.

For instance, we write

from random import shuffle

x = [[i] for i in range(10)]
shuffle(x)

to call shuffle with list x to shuffle the items in x in place.

Conclusion

To shuffle a list of objects with Python, we can use the shuffle function.

Categories
Python Answers

How to get a map() to return a list in Python 3.x?

Sometimes, we want to get a map() to return a list in Python 3.x.

In this article, we’ll look at how to get a map() to return a list in Python 3.x.

How to get a map() to return a list in Python 3.x?

To get a map() to return a list in Python 3.x, we can use the list function.

For instance, we write

l = list(map(chr, [66, 53, 0, 94]))

to call list with the map returned by map to convert the map to a list.

Conclusion

To get a map() to return a list in Python 3.x, we can use the list function.

Categories
Python Answers

How to run a Python script from PHP?

Sometimes, we want to run a Python script from PHP.

In this article, we’ll look at how to run a Python script from PHP.

How to run a Python script from PHP?

To run a Python script from PHP, we can use the shell_exec function.

For instance, we write

$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;

to call escapeshellcmd to escape the command string.

Then we call shell_exec to run the $command.

And we get the output from $output.

Conclusion

To run a Python script from PHP, we can use the shell_exec function.