[英]Python : list comprehension + filter
I have a string from which I wish to extract vowels and consonants along with their indices. 我有一个字符串,我希望从中提取元音和辅音及其索引。
I am thinking about : 我在考虑:
a = "HELLO"
list_ = list(a)
vow = [(x, i) for i,x in enumerate(list_) if x in ['A', 'E', 'I', 'O', 'U']]
cons = [(x, i) for i, x in enumerate(list_) if x not in j for j in list_ if j in ['A', 'E', 'I', 'O', 'U']]
However, I am getting NameError
(saying j
is not defined). 但是,我收到NameError
(说j
没有定义)。 Why am I not able to nest my list comprehension. 为什么我无法嵌套我的列表理解。
My desired output : 我想要的输出:
vows : [('E', 1), ('O', 4)]
cons : [('H', 0), ('L', 2), ('L', 3)]
The issue with your second list comprehension is that ideally the if
condition should go at the end (after the second for
loop , only then j
would we accessible). 你的第二个列表理解的问题是理想情况下if
条件应该结束(在第二个for
循环之后,只有j
才能访问)。 But you really do not need that, simply check if x
is not in the list of vowels. 但你真的不需要它,只需检查x
是否不在元音列表中。 Example - 示例 -
cons = [(x, i) for i,x in enumerate(list_) if x not in ['A', 'E', 'I', 'O', 'U']]
Demo - 演示 -
>>> a = "HELLO"
>>> list_ = list(a)
>>> vow = [(x, i) for i,x in enumerate(list_) if x in ['A', 'E', 'I', 'O', 'U']]
>>> cons = [(x, i) for i,x in enumerate(list_) if x not in ['A', 'E', 'I', 'O', 'U']]
>>> vow
[('E', 1), ('O', 4)]
>>> cons
[('H', 0), ('L', 2), ('L', 3)]
You can make this a bit faster by using set
for vowels
and you do not really need list_
, you can enumerate over a
itself, and get exactly same result. 你可以通过使用set
for vowels
来加快速度,你不需要list_
,你可以枚举a
自己,并获得完全相同的结果。 Example - 示例 -
vowel_set = {'A', 'E', 'I', 'O', 'U'}
vow = [(x, i) for i,x in enumerate(a) if x in vowel_set]
cons = [(x, i) for i,x in enumerate(a) if x not in vowel_set]
Demo - 演示 -
>>> a = "HELLO"
>>> vowel_set = {'A', 'E', 'I', 'O', 'U'}
>>> vow = [(x, i) for i,x in enumerate(a) if x in vowel_set]
>>> cons = [(x, i) for i,x in enumerate(a) if x not in vowel_set]
>>> vow
[('E', 1), ('O', 4)]
>>> cons
[('H', 0), ('L', 2), ('L', 3)]
You're over-complicating it 你太复杂了
a = "HELLO"
list_ = list(a)
vow = [(x, i) for i,x in enumerate(list_) if x in ['A', 'E', 'I', 'O', 'U']]
cons = [(x, i) for i, x in enumerate(list_) if x not in ['A', 'E', 'I', 'O', 'U']]
See Anand S Kumar's answer for details :) 有关详细信息,请参阅Anand S Kumar的答案:)
The mistake in your nesting is that you need to nest it again 嵌套中的错误是你需要再次嵌套它
cons = [(x, i) for i, x in enumerate(list_) if x not in [j for j in list_ if j in ['A', 'E', 'I', 'O', 'U']]]
Rule #1 about nested LCs in Python: The outer loop goes first. 关于Python中嵌套LC的规则#1: 外部循环首先出现。
cons = [(x, i) for j in list_ if j in ['A', 'E', 'I', 'O', 'U'] for i, x in enumerate(list_) if x not in j]
But this will give the wrong result since you should just be using not in
in the first place instead. 但这会产生错误的结果,因为你应该首先使用not in
。
The correct syntax would be to explicitely make the inner loop a list comprehension: 正确的语法是明确地使内循环成为列表理解:
cons = [(x, i) for i, x in enumerate(list_) if x not in [ j for
j in list_ if j in ['A', 'E', 'I', 'O', 'U']]]
But here the correct way to get consonants would be simply (as already said by others): 但是这里获得辅音的正确方法很简单(正如其他人已经说过的那样):
cons = [(x, i) for i,x in enumerate(list_) if x not in ['A', 'E', 'I', 'O', 'U']]
You can check for membership in a string: 您可以检查字符串中的成员身份:
>>> 'I' in 'AEIOU'
True
You could convert each character in a string to a member of a set, for slightly faster searching: 您可以将字符串中的每个字符转换为集合的成员,以便稍快搜索:
>>> 'Z' in set('AEIOU')
False
You don't need the second inner loop: 你不需要第二个内循环:
>>> vowels = set('AEIOU')
>>> vows = [(char, index) for index, char in enumerate(word) if char in vowels]
>>> cons = [(char, index) for index, char in enumerate(word) if char not in vowels]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.