简体   繁体   English

如何在满足给定条件的字符串列表列表中返回计算字符串总数的整数列表?

[英]How to return a list of integers that count total number of strings in a list of list of list of strings that satisfy a given condition?

I am trying to get a list of integers that return the number of strings in a list of list of list of strings which is not alphabet.我正在尝试获取一个整数列表,该列表返回非字母表的字符串列表列表中的字符串数。 eg:例如:

count_number([[['B0', 'N1','C']], [['AT', '1', 'K']]])

This should give me a result of这应该给我一个结果

[2,1]

What I tried is below: I've only managed to get the total number of strings in the whole list.我试过的内容如下:我只设法获得了整个列表中的字符串总数。

count = 0
    for sublist in List1:
        for words in sublist:
            for item in words:
                if not item.isalpha():
                    count +=1
return count

How to turn this into a list of int?如何将其转换为 int 列表? Any help will be appreciated.任何帮助将不胜感激。

you can use a regular expression:您可以使用正则表达式:

import re

l = [[['B0', 'N1','C']], [['AT', '1', 'K']]]

[sum(bool(re.search('[^a-zA-Z]', w)) for w in e) for i in l for e in i]

output:输出:

[2, 1]

Please check this.请检查这个。


def count_number(List1):
    out_put_list = []
    for sublist in List1:
        for words in sublist:
            count = 0
            for item in words:
                if not item.isalpha():
                    count +=1
            out_put_list.append(count)
    return out_put_list


print(count_number([[['B01', 'N1','C1'], ['B01', 'N1','C1']], [['AT1', '1', 'K']]]))

Output:输出:

[3, 3, 2]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM