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.
- First I get a list of all keys which end in
'_x1'. - Next, I strip
'_x1'from all of them usingrstrip('_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?
The parameter to
rstrip()is a set of characters to be stripped, not an exact string:General hint: If you suspect a bug in some function, read its documentation.