你好,我试着做一个电子邮件解析器,但它的行为很奇怪,谁能告诉我为什么
def parser(wrds):
s = set()
for wrd in wrds:
splited_wrd = wrd.split(' ')
for i in range(len(splited_wrd)):
wrds_indexed = splited_wrd[i]
if ('@' and '.' in wrds_indexed) and (len(wrds_indexed) > 13):
s.add(wrds_indexed)
yield from s
x = 'heykidhello@gmail.com', 'heykidgmail.com', 'heykidhello@gmailcom','h@gmail.com'
print(list(parser(x)))
输出:
['heykidgmail.com', 'heykidhello@gmail.com']
>>>
谁能告诉我为什么heykidgmail.com在列表中,而它显然没有"@">
>>> '@' and '.' in 'heykidgmail.com'
True
当我在控制台中运行它时,为什么当@不在单词中时它显示为True ?
'@' and '.' in 'heykidgmail.com'
并不像你想象的那样。
被解释为
bool('@') and bool('.' in 'heykidgmail.com')
或
True and bool('.' in 'heykidgmail.com')
因此,
bool('.' in 'heykidgmail.com')
您需要独立测试两个:
s = 'heykidgmail.com'
'@' in s and '.' in s