Remove punctuation from string in Python

Say we have a string “Hello, are you still there?”, we want to transform it into “Hello are you still there”, the question is how to do it?

Before we go ahead, we need to first define what punctuation is, it’s easy to do this by using string.punctuation. Let’s check its value using following command:

import string
print(string.punctuation)

Above code just outputs a string containing all punctuation, its content as follows:

'!"#$%&\'()*+,-./:;?@[\\]^_`{|}~'

Now let’s make the transformation using str.translate, see following code for details:

import string

# Create a string to operate on
s = "Hello, are you still there?"

# Create a translation table
translator = str.maketrans('', '', string.punctuation)

# Make the translate
s = s.translate(translator)

# Check the result
print(s)  # prints "Hello are you still there"

References

Advertisements

Python List Sorting Techniques

  1. How do I sort a list ?

You can sort a list by using the built-in list.sort() method that modifies the list in-place, and it’s stable. The other way to sort a list is via built-in function sorted() that builds a new sorted list. Let me give you two examples.

Example using list.sort():

import random
# a list with 10 integers
nums = [num for num in range(10)]
print(nums)
# shuffle nums to see the effect of sorting, note that this is a in-place operation
random.shuffle(nums)
print(nums)
# sort it
nums.sort()
print(nums)

The above piece of code will have an output as following:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 7, 5, 3, 6, 8, 4, 1, 9, 2]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Note: you may have different output per run because of the shuffle operation.

Example using sorted():

import random
# a list with 10 integers
nums = [num for num in range(10)]
print(nums)
# shuffle nums to see the effect of sorting, note that this is a in-place operation
random.shuffle(nums)
print(nums)
# sort it using sorted
nums_sorted = sorted(nums)  # a new list returned
print(nums_sorted)

Here, we’ll have a similar output as in the previous example:

[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[4, 5, 3, 7, 6, 2, 9, 8, 1, 0]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  1. How do I sort a list with multiple keys?

Suppose you have a list of records with each record representing a student. A student consists of a name, a gender, an age, etc. For simplicity, we can assume the record just consists of the three attributes aforementioned, thus the list looks like the following:

students = [('john', 'male', 23), ('alice', 'female', 18)]

In this case, you would like to sort the students first by name, then by the age. How can you do it? The answer is you can either use list.sort() or sorted(), but this time you need to supply a comparison function or specify the key to sort. Before we go ahead, let’s check the signatures:

The signature of list.sort() is sort(*, key=None, reverse=None) while the signature of sorted() is sorted(iterable[,key][,reverse].

Now let’s sort a list with multiple keys, see following for an example:

Here I only give an example using list.sort().

Example 1: specify key using a lambda

from pprint import pprint

# create a list to sort
students = [('dave', 'male', 10), ('jane', 'female', 12), ('john', 'male', 15)]
# specify key using a lambda
students.sort(key=lambda student: (student[0], student[2]))
pprint(students)

The above code gives us following output:

[('dave', 'male', 10), ('jane', 'female', 12), ('john', 'male', 15)]

You can see that it just works as we expected. Let’s check another example:

Example 2: specify key using itemgetter

from operator import itemgetter
from pprint import pprint

# create a list to sort
students = [('dave', 'male', 10), ('jane', 'female', 12), ('john', 'male', 15)]
# specify key using a lambda
students.sort(key=itemgetter(0, 2))
pprint(students)

This example has a same output as in example 1. There is a third way to do it, we can explicitly sort the students first by name, then by the age. Let’s have a look at it:

Example 3: explicitly do the sorting

from operator import itemgetter
from pprint import pprint

# create a list to sort
students = [('dave', 'male', 10), ('jane', 'female', 12), ('john', 'male', 15)]
students.sort(key=itemgetter(0))  # sort by name
students.sort(key=itemgetter(2), reverse=True) # sort by age in descending order
pprint(students)

The output of above code:

[('john', 'male', 15), ('jane', 'female', 12), ('dave', 'male', 10)]

This time we have a different output from previous two examples, we first sort by name, then sort by age in descending order. When we want to have fine controls over the order to sort, we use this method.

Welcome your comments!

deque in Python

deque是一种容器数据类型,包含在collections模块中。deque是一种类似list(list-like)的容器,具备在任一端快速插入和删除元素的特性。

deques是栈和队列的一种通用形式(deque发音为”deck”,是double-ended queu的简称),具有以下特点:

  • Thread-safe
  • Memory efficient,在任一端插入和删除元素的复杂度近似为O(1)
  • maxlen指定deque可以保存的元素数目上限,如果不指定,deque可以保存任意多的元素,否则仅能保存maxlen个元素,这种情况下在deque为满时,任一端的插入导致另一端元素的删除。

使用

导入deque:

from collections import deque

deque构造函数

class collections.deque([iterable[, maxlen]])