1
seki 2016-04-17 20:13:33 +08:00 1
|
2
SErHo 2016-04-17 20:14:01 +08:00 4
["abcdefghigklmnopqrstuvwxyz"[i: i+3] for i in range(0, len("abcdefghigklmnopqrstuvwxyz"), 3)]
|
3
aec4d 2016-04-17 20:48:10 +08:00 1
re.findall('.{3}','abcdefghigklmnopqrstuvwxyz')
|
4
billion 2016-04-17 20:51:16 +08:00
2 楼正解。
|
5
hcwhan 2016-04-17 20:53:27 +08:00 via Android
2 楼这样 如果不是 3 的倍数会报索引超出 错误吧
|
6
wittyfox 2016-04-17 21:20:35 +08:00 via Android
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"] |
7
wittyfox 2016-04-17 21:26:22 +08:00 via Android
如果核心库更完善的话,像这样:
```ruby String.class_eval do include Enumerable alias :each :each_char end ``` 那就可以这样: ```ruby str.each_slice(3).to_a ``` |
10
sjtlqy 2016-04-17 21:36:56 +08:00
|
11
xuboying 2016-04-17 21:44:16 +08:00
能用正则就用正则啊
#!python import re x="abcdefghijklmnopqrstuvwxyz" print re.findall(r".{1,3}",x) |
12
xcodebuild 2016-04-17 21:46:21 +08:00
JavaScript 版:
'abcdefghigklmnopqrstuvwxyz'.match(/.{3}/g) // => ["abc", "def", "ghi", "gkl", "mno", "pqr", "stu", "vwx"] |
13
ty0716 2016-04-17 21:54:11 +08:00
>>> import textwrap
>>> end.join(textwrap.wrap(body, chunklen)) https://docs.python.org/2/library/textwrap.html |
14
SakuraSa 2016-04-17 22:04:47 +08:00
以前用过一个奇怪的方法实现:
```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'] |
15
leavic 2016-04-17 22:22:06 +08:00
from more_itertools import chunked
搞定了 |
17
araraloren 2016-04-18 10:58:58 +08:00
~~ 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) ``` |
18
liyj144 2016-04-18 13:43:52 +08:00
参考 2 楼,加上末尾的处理: re.findall('.{3}|.{2}$','abcdefghigklmnopqrstuvwxyz')
|
20
HustLiu 2016-04-18 14:32:13 +08:00
@codefalling 可以优化一下 /.{1,3}/g ,这样避免字符串长度不是 3 的倍数出现遗漏
|
21
jackal 2016-04-18 18:11:06 +08:00
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> |
23
irenicus 2016-04-18 20:23:49 +08:00 via Android
|
24
xcodebuild 2016-04-18 21:03:06 +08:00
@HustLiu 有道理
|
25
extreme 2016-04-18 22:00:07 +08:00
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++; } |
26
saxon 2016-04-19 08:56:44 +08:00
s = "abcdefghijklmnopqrstuvwxyz"
print zip(s[::3]+s[1::3]+s[2::3]) :) |
27
saxon 2016-04-19 09:03:24 +08:00
更正,刚手滑回车
s = "abcdefghijklmnopqrstuvwxyz" def func(s): l = list(s) return ''.join(l) print map(func,zip(s[::3],s[1::3],s[2::3])) |
28
dofine 2016-04-20 00:31:19 +08:00 via iPad
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
whnzy OP 我想是不是应该再来点 C++,Golang,C#,OC,erlang,Java,Haskell.........
|