Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug in Python's str.rstrip() function, or my own stupidity?

Either this is a bug, or I'm about to learn something new about how Python behaves. :)

I have a dictionary filled with key/value pairs. Each key has a unique prefix, ias_XX_XX_. I'm attempting to get a list of every unique prefix in the dictionary.

  1. First I get a list of all keys which end in '_x1'.
  2. Next, I strip '_x1' from all of them using rstrip('_x1').

This works fine for all of them, except for the last one, ias_1_1_x1. Instead of being stripped to ias_1_1, it becomes ias_. Run the code to see for yourself:

d = {
'ias_16_10_x2':     575, 
'ias_16_10_x1':     0, 
'ias_16_10_y1':     0, 
'ias_16_10_y2':     359,
'ias_16_9_x2':      575, 
'ias_16_9_x1':      0, 
'ias_16_9_y1':      18, 
'ias_16_9_y2':      341, 
'ias_1_1_y1':       0, 
'ias_1_1_y2':       359,  
'ias_1_1_x2':       467, 
'ias_1_1_x1':       108,
}

x1_key_matches = [key for key in d if '_x1' in key]
print x1_key_matches

unique_ids = []
for x1_field in x1_key_matches:
    unique_ids.append(x1_field.rstrip('_x1'))

print unique_ids

Actual Output: (Python 2.6, 2.7, and 3.2 (must change print to print() for 3.x to work))

['ias_16_10_x1', 'ias_16_9_x1', 'ias_1_1_x1']
['ias_16_10', 'ias_16_9', 'ias']   # <<<--- Why isn't this last one ias_1_1???

Expected Output:

['ias_16_10_x1', 'ias_16_9_x1', 'ias_1_1_x1']
['ias_16_10', 'ias_16_9', 'ias_1_1']

If I change the key's name from ias_1_1 to something like ias_1_2, or ias_1_3, the glitch doesn't occur. Why is this happening?

like image 268
Dave Avatar asked Jun 25 '11 18:06

Dave


People also ask

What does Rstrip () do in Python?

Python String rstrip() Method The rstrip() method removes any trailing characters (characters at the end a string), space is the default trailing character to remove.

Does the Rstrip () function contain any arguments?

The rstrip() removes characters from the right based on the argument (a string specifying the set of characters to be removed). If chars argument is not provided, all whitespaces on the right are removed from the string.

What does Lstrip and Rstrip do in Python?

rstrip(): returns a new string with trailing whitespace removed. It's easier to remember as removing white spaces from “right” side of the string. lstrip(): returns a new string with leading whitespace removed, or removing whitespaces from the “left” side of the string.

What is text Strip ()?

The strip() method removes any leading (spaces at the beginning) and trailing (spaces at the end) characters (space is the default leading character to remove)


2 Answers

The parameter to rstrip() is a set of characters to be stripped, not an exact string:

>>> "abcbcbaba".rstrip("ab")
"abcbc"

General hint: If you suspect a bug in some function, read its documentation.

like image 111
Sven Marnach Avatar answered Oct 05 '22 10:10

Sven Marnach


From the docs, emphasis added:

The chars argument is a string specifying the set of characters to be removed. If omitted or None, the chars argument defaults to removing whitespace. The chars argument is not a suffix; rather, all combinations of its values are stripped.

like image 25
senderle Avatar answered Oct 05 '22 10:10

senderle