Categories
Python Answers

How to slice 2d array into smaller 2d arrays with Python?

Sometimes, we want to slice 2d array into smaller 2d arrays with Python.

In this article, we’ll look at how to slice 2d array into smaller 2d arrays with Python.

How to slice 2d array into smaller 2d arrays with Python?

To slice 2d array into smaller 2d arrays with Python, we can use the NumPy split method.

For instance, we write

a = np.arange(30).reshape([5,6])
a1 = np.split(a,3,axis=1) 

to create an array with arange.

Then we call reshape to reshape the returned array to a 5×6 2d array.

Next, we call split with array a, 3 and the axis` argument set to 1 to get a list of 3 5×2 arrays.

Conclusion

To slice 2d array into smaller 2d arrays with Python, we can use the NumPy reshape method.

Categories
Python Answers

How to import Lodash into an Angular TypeScript application?

Sometimes, we want to import Lodash into an Angular TypeScript application.

In this article, we’ll look at how to import Lodash into an Angular TypeScript application.

How to import Lodash into an Angular TypeScript application?

To import Lodash into an Angular TypeScript application, we can install the lodash package.

To install it, we run

npm install --save lodash
npm install --save-dev @types/lodash

to install the lodash package and the TypeScript type definitions for the lodash package respectively.

Then we import it by adding

import _ from "lodash";

in our Angular app’s code.

Conclusion

To import Lodash into an Angular TypeScript application, we can install the lodash package.

Categories
Python Answers

How to normalize Unicode with Python?

Sometimes, we want to normalize Unicode with Python.

In this article, we’ll look at how to normalize Unicode with Python.

How to normalize Unicode with Python?

To normalize Unicode with Python, we call unicodedata.normalize.

For instance, we write

print(ascii(unicodedata.normalize('NFC', '\u0061\u0301')))

to call the unicodedata.normalize method to normalize'\u0061\u0301'.

We call it with 'NFC' to return composed characters.

Then we call ascii to ensure that non-ASCII codepoints are printed using escape syntax.

Conclusion

To normalize Unicode with Python, we call unicodedata.normalize.

Categories
Python Answers

How to convert XML/HTML entities into Unicode string in Python?

Sometimes, we want to convert XML/HTML entities into Unicode string in Python.

In this article, we’ll look at how to convert XML/HTML entities into Unicode string in Python.

How to convert XML/HTML entities into Unicode string in Python?

To convert XML/HTML entities into Unicode string in Python, we can use the html.unescape method.

For instance, we write

import html

a = html.unescape('©')
b = html.unescape('©')

to call html.unescape with strings to decode the string with the XML or HTML entities into Unicode strings.

Conclusion

To convert XML/HTML entities into Unicode string in Python, we can use the html.unescape method.

Categories
Python Answers

How to ping a site in Python?

Sometimes, we want to ping a site in Python.

In this article, we’ll look at how to ping a site in Python.

How to ping a site in Python?

To ping a site in Python, we can use the ping.verbose_ping method.

For instance, we write

import ping, socket
try:
    ping.verbose_ping('www.google.com', count=3)
    delay = ping.Ping('www.wikipedia.org', timeout=2000).do()
except socket.error, e:
    print(e)

to call ping.verbose_ping to ping www.google.com 3 times.

And we ping www.wikipedia.com with

ping.Ping('www.wikipedia.org', timeout=2000).do()

And we time out the ping in 2 seconds by setting the timeout argument to 2000.

We call do to start the ping.

Conclusion

To ping a site in Python, we can use the ping.verbose_ping method.