#26222: Python 進階寫法:itertools groupby + list comprehension


406490150@gms.tku.edu.tw (我是朱朱)

School : 國立交通大學
ID : 139794
IP address : [140.113.236.122]
Last Login :
2021-10-14 15:49:00
a011. 00494 - Kindergarten Counting Game -- UVa494 | From: [114.39.244.128] | Post Date : 2021-07-25 01:22

groupby(s, str.isalpha) -> flag(true or false),  iterator(用不到)

https://docs.python.org/3/library/itertools.html#itertools.groupby

 

利用 list comprehension

sum(k%2 for k in range(10)) # 只加奇數

->推廣到此題 只要 flag==True (是單字)  就 +1

 

寫完大概長這樣,四行

 

 

import sys
from itertools import groupby

for each line:
    print(sum(write your list comprehension here))
 
ZeroJudge Forum