Python 的 str.rstrip() 函数中的错误,还是我自己的愚蠢?
这要么是一个错误,要么我即将学习有关 Python 行为方式的新知识。 :)
我有一本充满键/值对的字典。每个键都有一个唯一的前缀,ias_XX_XX_
。我试图获取字典中每个唯一前缀的列表。
- 首先,我得到以
'_x1'
结尾的所有键的列表。 - 接下来,我使用
rstrip('_x1'
) 从所有这些中剥离'_x1'
。
除了最后一个 ias_1_1_x1
之外,这对所有这些都适用。它不会被剥离为 ias_1_1
,而是变为 ias_
。运行代码亲自查看:
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
实际输出:(Python 2.6、2.7 和 3.2(必须将 print 更改为 print() 才能使 3.x 正常工作))
['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???
预期输出:
['ias_16_10_x1', 'ias_16_9_x1', 'ias_1_1_x1']
['ias_16_10', 'ias_16_9', 'ias_1_1']
如果我将密钥名称从 ias_1_1
更改为 ias_1_2
或 ias_1_3
之类的名称,则不会出现故障。为什么会发生这种情况?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
rstrip()
的参数是一组要删除的字符,而不是精确的字符串:一般提示:如果您怀疑某些函数存在错误,请阅读其 文档。
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.
从 docs 中,添加了强调:
From the docs, emphasis added:
.rstrip的参数不是我们要删除的字符串,而是我们要删除的字符。检查示例:
.rstrip's parameter isn't the string which we want to strip, it's the characters we want to strip. Check that examples:
.rstrip()
删除匹配字符的所有组合,而不是您提供的实际字符串。请参阅http://docs.python.org/library/stdtypes.html。.rstrip()
removes all combinations of matching characters, not the actual string you provide. See http://docs.python.org/library/stdtypes.html.试试这个:
Try this out instead:
rstrip 返回删除了尾随字符的字符串副本。
例如:
如果您正在处理文件名,请格外小心,
希望这会有所帮助!
rstrip returns a copy of the string with trailing characters removed.
For example:
If you are dealing with file names be extra careful,
Hope this helps!