[英]Difference between [y for y in x.split('_')] and x.split('_')
I've found this question and one thing in the original code bugs me: 我发现了这个问题 ,原始代码中的一件事让我感到困惑:
>>> x="Alpha_beta_Gamma"
>>> words = [y for y in x.split('_')]
What's the point of doing this: [y for y in x.split('_')]
? 这样做有什么意义: [y for y in x.split('_')]
? split
already returns a list and items aren't manipulated in this list comprehension. split
已经返回一个列表,并且在此列表理解中不会操纵项目。 Am I missing something? 我错过了什么吗?
You're correct; 你是对的; there's no point in doing that. 这样做没有意义。 However, it's often seen in combination with some kind of filter or other structure, such as [y for y in x.split('_') if y.isalpha()]
. 然而,它经常与某种过滤器或其他结构结合使用,例如[y for y in x.split('_') if y.isalpha()]
。
There is no difference in result but using list comprehension in this case in not a good way and in redundant! 结果没有区别,但在这种情况下使用列表理解并不是一个好方法和冗余!
>>> x="Alpha_beta_Gamma"
>>> [y for y in x.split('_')]
['Alpha', 'beta', 'Gamma']
>>> x.split('_')
['Alpha', 'beta', 'Gamma']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.