简体   繁体   English

为什么我得到这些不同的输出?

[英]Why I am getting these different outputs?

print('xyxxyyzxxy'.lstrip('xyy'))
# output:zxxy

print("xyxefgooeeee".lstrip("efg"))
# ouput:xyxefgooeeee

print('reeeefooeeee'.lstrip('eeee'))
# output:reeeefooeeee

Here for the last two print statements, I am expecting output as a first print statement, as it has stripped 'xyxxyy', but in the last two print statements, it is not stripping in the same way as it has done in first.对于最后两个打印语句,我期望 output 作为第一个打印语句,因为它已经剥离了“xyxxyy”,但在最后两个打印语句中,它并没有像第一次那样剥离。 Please tell me why it so?请告诉我为什么会这样?

In Python leading characters in Strings containing xyy are removed because of.lstrip().在 Python 中,包含 xyy 的字符串中的前导字符因 .lstrip() 而被删除。 For example:例如:

txt = ",,,,,ssaaww.....banana"

x = txt.lstrip(",.asw")

print(x)

The output will be: banana output 将是:香蕉

string.lstrip(chars) removes characters from the left size of the string until it reached a character that does not appear in chars . string.lstrip(chars)从字符串的左侧大小中删除字符,直到它到达一个未出现在chars中的字符。

In your second and third examples, the first character of the string does not appear in chars , so no characters are removed from the string.在您的第二个和第三个示例中,字符串的第一个字符没有出现在chars中,因此不会从字符串中删除任何字符。

I think because the order of char doesn't matter.我认为因为 char 的顺序无关紧要。

xyy or yxx will result in the same thing. xyyyxx将导致相同的结果。 It will remove chars from the left side until it sees a char that is not included.它将从左侧删除字符,直到看到未包含的字符。 For example:例如:

print('xyxxyyzxxy'.lstrip('xyy'))
zxxy

print('xyxxyyzxxy'.lstrip('yxx'))
zxxy

In fact, if you only used 2 chars 'xy' or 'yx' you will get the same thing:事实上,如果你只使用 2 个字符 'xy' 或 'yx' 你会得到同样的结果:

print('xyxxyyzxxy'.lstrip('xy'))
zxxy

In the other cases, the first left char is not included, therefore there's no stripping在其他情况下,不包括第一个左字符,因此没有剥离

I just got to know lstrip() removes, all combinations of the characters passed as an argument are removed from the left-hand side.我刚刚知道 lstrip() 删除,作为参数传递的所有字符组合都从左侧删除。

lstring using the set of the chars in the string and then removes the all characters from the primary string start from the left lstring 使用字符串中的字符集,然后从左侧开始的主字符串中删除所有字符

print('xyxefgooeeee'.lstrip('yxefg')) 
"""In 'xyxefgooeeee' the first char is 'x' and it exists in the 'yxefg' so 
will be removed and then it will move to the next char 'y','x','x','e','f', 
'g' and then 'o' which doesn't exist. therefore will return string after 'o'
"""
OutPut : ooeeee

print('xyxefgooeeee'.lstrip('efg'))
"""In the xyxefgooeeee' the first char 'x' does to exist in the 'efg' so will
not be removed and will not move to the next char and will return the
entire primary string
"""
OutPut: xyxefgooeeee

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我会为同一个数据集得到不同的输出? - Why am I getting different outputs for the same dataset? 为什么对于(看似)相同的代码我得到不同的输出? - Why am I getting different outputs for the (seemingly) same code? 为什么类似的输入会得到不同的输出? - Why am I getting different outputs for a similar input? 为什么我得到多个输出? - Why am I getting multiple outputs? 为什么我使用 Python function 和 Numpy 内置点() Python function 得到 2 个不同的输出 - Why I am getting 2 different outputs for same calculation using Python function and Numpy inbuilt dot() function 为什么我得到两个不同的输出? - Why I'm getting two different outputs? 我如何在不同的 Jupyter 笔记本上获得不同的输出? - How am I getting different outputs on different Jupyter notebooks? 为什么使用此修饰功能可以获得多个输出? - Why am I getting multiple outputs with this decorated function? 尽管提供了整数输入,为什么我只得到“非整数”输出? - Why am I getting only "Non-int" outputs despite giving integar inputs? 为什么我从不同位置收到不同的 http 响应? - Why am I getting different http responses from different locations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM