假设我有一小段代码:
<?php
$tmp = str_split('hello world!',2);
// $tmp will now be: array('he','ll','o ','wo','rl','d!');
foreach($tmp AS &$a) {
// some processing
}
unset($tmp);
?>
Run Code Online (Sandbox Code Playgroud)
我怎么能在Python v2.7中做到这一点?
我以为这样做会:
the_string = 'hello world!'
tmp = the_string.split('',2)
for a in tmp:
# some processing
del tmp
Run Code Online (Sandbox Code Playgroud)
但它返回一个"空分隔符"错误.
有什么想法吗?
for i in range(0, len(the_string), 2):
print(the_string[i:i+2])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
829 次 |
| 最近记录: |