Python 的 str.rstrip() 函数中的错误,还是我自己的愚蠢?

发布于 2024-11-17 04:37:30 字数 1427 浏览 10 评论 0原文

这要么是一个错误,要么我即将学习有关 Python 行为方式的新知识。 :)

我有一本充满键/值对的字典。每个键都有一个唯一的前缀,ias_XX_XX_。我试图获取字典中每个唯一前缀的列表。

  1. 首先,我得到以 '_x1' 结尾的所有键的列表。
  2. 接下来,我使用 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_2ias_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.

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不爱素颜 2024-11-24 04:37:30

rstrip() 的参数是一组要删除的字符,而不是精确的字符串:

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

一般提示:如果您怀疑某些函数存在错误,请阅读其 文档

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.

你怎么这么可爱啊 2024-11-24 04:37:30

docs 中,添加了强调:

chars 参数是一个字符串,指定要删除的字符集。如果省略或为 None,则 chars 参数默认删除空格。 chars 参数不是后缀;相反,它的值的所有组合都会被删除。

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.

软的没边 2024-11-24 04:37:30

.rstrip的参数不是我们要删除的字符串,而是我们要删除的字符。检查示例:

>>> "12345678".rstrip("158")
'1234567'
>>> "12345678".rstrip("asd8qwe")
'1234567'
>>> "12345678".rstrip("78")
'123456'
>>> "1234568788".rstrip("78")
'123456'

.rstrip's parameter isn't the string which we want to strip, it's the characters we want to strip. Check that examples:

>>> "12345678".rstrip("158")
'1234567'
>>> "12345678".rstrip("asd8qwe")
'1234567'
>>> "12345678".rstrip("78")
'123456'
>>> "1234568788".rstrip("78")
'123456'
百变从容 2024-11-24 04:37:30

.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.

热血少△年 2024-11-24 04:37:30

试试这个:

unique_ids.append(re.sub('_x1
, '', x1_field)

Try this out instead:

unique_ids.append(re.sub('_x1
, '', x1_field)
ぃ双果 2024-11-24 04:37:30

rstrip 返回删除了尾随字符的字符串副本。

例如:

>>> '   spacious   '.rstrip()
'   spacious'
>>> "AABAA".rstrip("A")
'AAB'
>>> "ABBA".rstrip("AB") # both AB and BA are stripped
''
>>> "ABCABBA".rstrip("AB")
'ABC'

########

>>> '   spacious   '.rstrip()
'   spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'

如果您正在处理文件名,请格外小心,

>>> "cosmac.csv".replace(".csv")
'cosma'
>>> "cosmac.csv".replace(".csv", "")
'cosmac'

希望这会有所帮助!

rstrip returns a copy of the string with trailing characters removed.

For example:

>>> '   spacious   '.rstrip()
'   spacious'
>>> "AABAA".rstrip("A")
'AAB'
>>> "ABBA".rstrip("AB") # both AB and BA are stripped
''
>>> "ABCABBA".rstrip("AB")
'ABC'

########

>>> '   spacious   '.rstrip()
'   spacious'
>>> 'mississippi'.rstrip('ipz')
'mississ'

If you are dealing with file names be extra careful,

>>> "cosmac.csv".replace(".csv")
'cosma'
>>> "cosmac.csv".replace(".csv", "")
'cosmac'

Hope this helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文