如题!
|
1
|
|
2
["abcdefghigklmnopqrstuvwxyz"[i: i+3] for i in range(0, len("abcdefghigklmnopqrstuvwxyz"), 3)]
|
|
3
re.findall('.{3}','abcdefghigklmnopqrstuvwxyz')
|
|
billionApr 17, 2016
4
2 楼正解。
|
|
hcwhanApr 17, 2016 via Android
5
2 楼这样 如果不是 3 的倍数会报索引超出 错误吧
|
|
wittyfoxApr 17, 2016 via Android
6
str = ('a'..'z').to_a.join
# => "abcdefghijklmnopqrstuvwxyz" str.split('').each_slice(3).to_a.map(&:join) => ["abc", "def", "ghi", "jkl", "mno", "pqr", "stu", "vwx", "yz"] |
|
wittyfoxApr 17, 2016 via Android
7
如果核心库更完善的话,像这样:
```ruby String.class_eval do include Enumerable alias :each :each_char end ``` 那就可以这样: ```ruby str.each_slice(3).to_a ``` |
|
sjtlqyApr 17, 2016
10
|
|
xuboyingApr 17, 2016
11
能用正则就用正则啊
#!python import re x="abcdefghijklmnopqrstuvwxyz" print re.findall(r".{1,3}",x) |
|
xcodebuildApr 17, 2016
12
JavaScript 版:
'abcdefghigklmnopqrstuvwxyz'.match(/.{3}/g) // => ["abc", "def", "ghi", "gkl", "mno", "pqr", "stu", "vwx"] |
|
chac88Apr 17, 2016
13
>>> import textwrap
>>> end.join(textwrap.wrap(body, chunklen)) https://docs.python.org/2/library/textwrap.html |
|
SakuraSaApr 17, 2016
14
以前用过一个奇怪的方法实现:
```python s,n='abcdefghigklmnopqrstuvwxyz',3 g=iter(s) print([''.join(i) for i in zip(*[g]*n)]) ``` >>> ['abc', 'def', 'ghi', 'gkl', 'mno', 'pqr', 'stu', 'vwx'] 要保留最后的 yz 的话,可以: ```python import itertools s,n='abcdefghigklmnopqrstuvwxyz',3 g=iter(s) print([''.join(j for j in i if j) for i in itertools.izip_longest(*[g]*n)]) ``` >>>['abc', 'def', 'ghi', 'gkl', 'mno', 'pqr', 'stu', 'vwx', 'yz'] |
|
leavicApr 17, 2016
15
from more_itertools import chunked
搞定了 |
|
araralorenApr 18, 2016
17
~~ perl6 版
```perl6 #!/usr/bin/env perl6 # 3 个一组合 say (comb /\w ** 3/, ['a' ... 'z'].join); # 输出 (abc def ghi jkl mno pqr stu vwx) # 保留 yz say (comb /\w ** 3 | \w ** 2/, ['a' ... 'z'].join); # 输出 (abc def ghi jkl mno pqr stu vwx yz) ``` |
|
liyj144Apr 18, 2016
18
参考 2 楼,加上末尾的处理: re.findall('.{3}|.{2}$','abcdefghigklmnopqrstuvwxyz')
|
|
HustLiuApr 18, 2016
20
@codefalling 可以优化一下 /.{1,3}/g ,这样避免字符串长度不是 3 的倍数出现遗漏
|
|
jackalApr 18, 2016
21
Javascript+正则表达式版本
<script type="text/javascript"> var str = "abcdefghigklmnopqrstuvwxyz"; var exp = /[a-z]{3}/g; var arr = []; var index = 0; while ((arr = exp.exec(str)) !== null) { console.log(arr[0]); index = exp.lastIndex; } console.log(str.substring(index)); </script> |
|
xcodebuildApr 18, 2016
24
@HustLiu 有道理
|
|
extremeApr 18, 2016
25
C 版:
int x = 0, y = 0; char *string ="abcdefghigklmnopqrstuvwxyz" , three_char[9][4] = {0}; while (*string != '\0') { three_char[x][y % 3] = *string; y++; if (y == 3) { y = 0; x++; } string++; } |
|
saxonApr 19, 2016
26
s = "abcdefghijklmnopqrstuvwxyz"
print zip(s[::3]+s[1::3]+s[2::3]) :) |
|
saxonApr 19, 2016
27
更正,刚手滑回车
s = "abcdefghijklmnopqrstuvwxyz" def func(s): l = list(s) return ''.join(l) print map(func,zip(s[::3],s[1::3],s[2::3])) |
|
dofineApr 20, 2016 via iPad
28
def grouper(iterable, n, fillvalue=None):
"Collect data into fixed-length chunks or blocks" # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx args = [iter(iterable)] * n return izip_longest(fillvalue=fillvalue, *args) |
|
29
我想是不是应该再来点 C++,Golang,C#,OC,erlang,Java,Haskell.........
|