作者healthlin (lin)
看板Python
标题[问题] lsit分割字串
时间Sat Sep 27 13:02:14 2014
如果我有list是如下:
['t','t','f','t','t','t','f','t']
要如何遇到f就切割呢 要计算t的连续长度
(这个例子是2,3,1)
谢谢
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 120.127.47.211
※ 文章网址: http://webptt.com/cn.aspx?n=bbs/Python/M.1411794136.A.942.html
1F:推 grtfor: 拜访list元素,一遇到'f'就把拜访次数yield出去 09/27 13:22
2F:→ bigpigbigpig: L1 = ['t','t','f','t','t','t','f','t'] 09/27 14:52
3F:→ bigpigbigpig: L2 = list(map(len, ''.join(L1).split('f'))) 09/27 14:52
4F:推 ckclark: from itertools import groupby 09/27 18:55
5F:→ ckclark: [len(list(v)) for k, v in groupby(data) if k == 't'] 09/27 18:55