Categories
Python Answers

How to remove duplicate characters from a string with Python?

Sometimes, we want to remove duplicate characters from a string with Python.

In this article, we’ll look at how to remove duplicate characters from a string with Python.

How to remove duplicate characters from a string with Python?

To remove duplicate characters from a string with Python, we can use the set and string join methods.

For instance, we write

bar = "".join(set(foo))

to call set foo to create set from string foo without the duplicate characters in foo.

Then we call ''.join with the returned set to join the characters in the set back into a string.

Conclusion

To remove duplicate characters from a string with Python, we can use the set and string join methods.

Categories
Python Answers

How to test whether a Numpy array contains a given row with Python?

Sometimes, we want to test whether a Numpy array contains a given row with Python.

In this article, we’ll look at how to test whether a Numpy array contains a given row with Python.

How to test whether a Numpy array contains a given row with Python?

To test whether a Numpy array contains a given row with Python, we can use the np.any method.

For instance, we write

np.any(np.all(a == b, axis=1))

to check any rows in a is the same as b by calling np.all with a == b.

And then we call np.any to check if there’re any such matches,.

Conclusion

To test whether a Numpy array contains a given row with Python, we can use the np.any method.

Categories
Python Answers

How to edit the date formatting of x-axis tick labels in matplotlib with Python?

Sometimes, we want to edit the date formatting of x-axis tick labels in matplotlib with Python.

In this article, we’ll look at how to edit the date formatting of x-axis tick labels in matplotlib with Python.

How to edit the date formatting of x-axis tick labels in matplotlib with Python?

To edit the date formatting of x-axis tick labels in matplotlib with Python, we can call the set_major_formatter method.

For instance, we write

import matplotlib.dates as mdates

myFmt = mdates.DateFormatter('%d')
ax.xaxis.set_major_formatter(myFmt)

to create a DateFormatter object that returns the 2 digit date from a date.

Then we call ax.xaxis.set_major_formatter with myFmt to set the formatter for the x-axis to myFmt to format dates to 2 digit dates.

Conclusion

To edit the date formatting of x-axis tick labels in matplotlib with Python, we can call the set_major_formatter method.

Categories
Python Answers

How to group Python dictionary keys as a list and create a new dictionary with this list as a value?

Sometimes, we want to group Python dictionary keys as a list and create a new dictionary with this list as a value.

In this article, we’ll look at how to group Python dictionary keys as a list and create a new dictionary with this list as a value.

How to group Python dictionary keys as a list and create a new dictionary with this list as a value?

To group Python dictionary keys as a list and create a new dictionary with this list as a value, we can loop through the dict items and call setdefault to set the default value for each key.

For instance, we write

v = {}

for key, value in sorted(d.items()):
    v.setdefault(value, []).append(key)

to create a new empty dict v.

Then we use a for loop to loop through the key-value pairs in dict d returned by d.items.

In the loop, we call v.setdefult to add a new entry with key value and value key.

Conclusion

To group Python dictionary keys as a list and create a new dictionary with this list as a value, we can loop through the dict items and call setdefault to set the default value for each key.

Categories
Python Answers

How to poll the keyboard to detect a keypress in Python?

Sometimes, we want to poll the keyboard to detect a keypress in Python.

In this article, we’ll look at how to poll the keyboard to detect a keypress in Python.

How to poll the keyboard to detect a keypress in Python?

To poll the keyboard to detect a keypress in Python, we can use the pynput library.

To install it, we run

pip install pynput

Then we write

from pynput.keyboard import Key, Listener

def on_press(key):
    print('{0} pressed'.format(
        key))

def on_release(key):
    print('{0} release'.format(
        key))
    if key == Key.esc:
        # Stop listener
        return False

with Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

to add event listeners for key presses and releases.

We create a Listener object using the on_press and on_release functions to listen for key presses and key releases respectively.

And we call listener.join to collect events until released.

We get the key pressed from the key parameter in each event handler function.

Conclusion

To poll the keyboard to detect a keypress in Python, we can use the pynput library.