Categories
Python Answers

How to check if a string contains an element from a list in Python?

Sometimes, we want to check if a string contains an element from a list in Python.

In this article, we’ll look at how to check if a string contains an element from a list in Python.

How to check if a string contains an element from a list in Python?

To check if a string contains an element from a list in Python, we can use the any function.

For instance, we write:

extensions_to_check = ['.txt', '.csv']
url_string = 'test.txt'
if any(ext in url_string for ext in extensions_to_check):
    print(url_string)

We use ext in url_string for ext in extensions_to_check to check if the url_string includes the string is in the extensions_to_check list.

And we use the returned generator with any to do the check.

Since this is True, we see 'test.txt' printed.

Conclusion

To check if a string contains an element from a list in Python, we can use the any function.

Categories
Python Answers

How to access the ith column of a Python NumPy multidimensional array?

Sometimes, we want to access the ith column of a Python NumPy multidimensional array.

In this article, we’ll look at how to access the ith column of a Python NumPy multidimensional array.

How to access the ith column of a Python NumPy multidimensional array?

To access the ith column of a Python NumPy multidimensional array, we can put the column index after :, in the square brackets.

For instance, we write:

import numpy

test = numpy.array([[1, 2], [3, 4], [5, 6]])
col = test[:, 1]
print(col)

We create the test NumPy array with a nested list.

Then we get the 2nd column of the NumPy array with test[:, 1] and assign the returned array to col.

Therefore, col is [2 4 6].

Conclusion

To access the ith column of a Python NumPy multidimensional array, we can put the column index after :, in the square brackets.

Categories
Python Answers

How to check if a word is an English word with Python?

Sometimes, we want to check if a word is an English word with Python.

In this article, we’ll look at how to check if a word is an English word with Python.

How to check if a word is an English word with Python?

To check if a word is an English word with Python, we can use the enchant module.

To install it, we run:

pip install pyenchant

Then we can use it by writing:

import enchant

d = enchant.Dict("en_US")
print(d.check("Hello"))
print(d.suggest("Helo"))

We return the enchant dictionary object with the enchant.Dict class with the locale string as its argument.

Then we call d.check with a string to check if the string is an English word.

And we also called d.suggest with a string to check if there’re any English words close to the string argument.

Therefore, we see:

True
['He lo', 'He-lo', 'Hello', 'Helot', 'Help', 'Halo', 'Hell', 'Held', 'Helm', 'Hero', "He'll"]

from the print output.

Conclusion

To check if a word is an English word with Python, we can use the enchant module.

Categories
Python Answers

How to compare two DataFrames and output their differences side-by-side with Python Pandas?

Sometimes, we want to compare two DataFrames and output their differences side-by-side with Python Pandas.

In this article, we’ll look at how to compare two DataFrames and output their differences side-by-side with Python Pandas.

How to compare two DataFrames and output their differences side-by-side with Python Pandas?

To compare two DataFrames and output their differences side-by-side with Python Pandas, we can use the data frame’s compare method.

For instance, we write:

import pandas as pd

df1 = pd.DataFrame({'a': [1, 2], 'b': [3, 4]})
df2 = pd.DataFrame({'a': [1, 2], 'b': [3, 5]})
changed = df1.compare(df2)
print(changed)

We create 2 data frames df1 and df2.

Then we call df1.compare with df2 to compare the difference between df1 and df2.

Therefore, changed is:

     b      
  self other
1  4.0   5.0

Conclusion

To compare two DataFrames and output their differences side-by-side with Python Pandas, we can use the data frame’s compare method.

Categories
Python Answers

How to retrieve parameters from a URL with Python?

Sometimes, we want to retrieve parameters from a URL with Python.

In this article, we’ll look at how to retrieve parameters from a URL with Python.

How to retrieve parameters from a URL with Python?

To retrieve parameters from a URL with Python, we can use the urllib module.

For instance, we write:

from urllib.parse import urlparse
from urllib.parse import parse_qs

url = 'https://www.example.com/some_path?some_key=some_value'
parsed_url = urlparse(url)
captured_value = parse_qs(parsed_url.query)['some_key'][0]

print(captured_value)

We have the url that we want to parse the query string part for.

To do that, we call urlparse with the url.

Then we call parse_qs on the query string part of the URL which is stored in parsed_url.query.

And finally we get the value by the key with parse_qs(parsed_url.query)['some_key'][0] .

Therefore, captured_value is 'some_value'.

Conclusion

To retrieve parameters from a URL with Python, we can use the urllib module.