gpt4 book ai didi

python - 在Python中,如何计算电子邮件地址列表中 '1'的数量?

转载 作者:行者123 更新时间:2023-11-28 21:34:19 24 4
gpt4 key购买 nike

这是我到目前为止所拥有的:

emails = ['<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ef4fff3fbedafdefbe6fff3eef2fbb0fdf1f3" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="c5afa4abb0a9acaba0f4f785afa4abb0a9acaba0eba6aaa8" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2a4c434646435a044745585843596a5a4704494547" rel="noreferrer noopener nofollow">[email protected]</a>',
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3f4d5e467f5d4d5e5b5d4a4d4611504d58" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b4d9d19fcddbc1f4dcdbc0d9d5ddd89ad7dbd9" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4d3e283b28237c7c0d2a202c2421632e2220" rel="noreferrer noopener nofollow">[email protected]</a>',
'<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="3856574a5d48544178554b5c165b5755" rel="noreferrer noopener nofollow">[email protected]</a>', '<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bedddfcccd8acbfecadbcdd2df90ddd1d3" rel="noreferrer noopener nofollow">[email protected]</a>']

def email_security_scan(a, b):
numbers = []
for item in a:
for subitem in item.split():
if subitem == b:
numbers.append +=1
print(numbers)

email_security_scan(emails, 1)

它不起作用,返回[]

最佳答案

你的逻辑有问题:

  • 您正在对没有空格的字符串使用 str.split。无需拆分字符串。
  • 您正在比较整数和字符串。这是行不通的。
  • 您将追加到列表中,而不是递增计数器变量。

因此,如果您需要计算包含 '1'电子邮件地址数量,这将起作用:

def email_security_scan(a, b):
count = 0
for item in a:
if str(b) in item:
count += 1
print(count)

email_security_scan(emails, 1)

# 3

更简单地说,您可以将 sum 与生成器表达式一起使用:

def email_security_scan(a, b):
print(sum(str(b) in item for item in a))

email_security_scan(emails, 1)

# 3

或者要计算所有电子邮件地址中“1”的总数,您可以使用 str.join,然后使用 str.count:

def email_security_scan(a, b):
print(''.join(a).count(str(b)))

email_security_scan(emails, 1)

# 4

关于python - 在Python中,如何计算电子邮件地址列表中 '1'的数量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53468018/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com