[英]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?
我正在嘗試獲取一個整數列表,該列表返回非字母表的字符串列表列表中的字符串數。 例如:
count_number([[['B0', 'N1','C']], [['AT', '1', 'K']]])
這應該給我一個結果
[2,1]
我試過的內容如下:我只設法獲得了整個列表中的字符串總數。
count = 0
for sublist in List1:
for words in sublist:
for item in words:
if not item.isalpha():
count +=1
return count
如何將其轉換為 int 列表? 任何幫助將不勝感激。
您可以使用正則表達式:
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]
輸出:
[2, 1]
請檢查這個。
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']]]))
輸出:
[3, 3, 2]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.