问个问题 'abcdefghigklmnopqrstuvwxyz'怎么切成 ['abc','def','ghi'…]这样的形式啊

2016-04-17 20:08:17 +08:00
 whnzy
如题!
5347 次点击
所在节点    Python
29 条回复
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>
irenicus
2016-04-18 20:20:29 +08:00
perl 版
@out = ("abcdef" =~ /(...)/);
irenicus
2016-04-18 20:23:49 +08:00
那个不行
@out = split /(...)/, "abcdefghijklmnopqrstuvwxyz";
不是三的倍数也行
xcodebuild
2016-04-18 21:03:06 +08:00
@HustLiu 有道理
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++;
}
saxon
2016-04-19 08:56:44 +08:00
s = "abcdefghijklmnopqrstuvwxyz"
print zip(s[::3]+s[1::3]+s[2::3])
:)
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]))
dofine
2016-04-20 00:31:19 +08:00
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)
whnzy
2016-04-23 12:25:56 +08:00
我想是不是应该再来点 C++,Golang,C#,OC,erlang,Java,Haskell.........

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/271802

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX