返回信息流abc=[location[i * (len + 1) + 1: (i + 1) * (len + 1) + 1] for i in range(rem)]
location是一个字符串,len和rem均是int类型数字,这就话是什么意思,尤其是后面的for[ema1]
这是一条镜像帖。来源:北邮人论坛 / python / #7659同步于 2015/6/29
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
[问题]看不懂的代码
Penguinbupt
2015/6/29镜像同步11 回复
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
mark,for还能这么用?!
【 在 Penguinbupt (会飞的企鹅+意涵团+非凡王者) 的大作中提到: 】
: abc=[location[i * (len + 1) + 1: (i + 1) * (len + 1) + 1] for i in range(rem)]
: location是一个字符串,len和rem均是int类型数字,这就话是什么意思,尤其是后面的for[ema1]
后面的for是list comprehension
location[xxxx:yyyy]是取一个列表的一部分,从xxxx到yyyy(不含yyyy)
Python的列表表达式。
range(rem)就是从0到rem-1的数组,间隔是1.
for in 相当于Java的 for each.
for in语句后面不做操作直接将每个i返回,返回的结果执行前面的那个操作。
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-43-generic x86_64)
* Documentation: https://help.ubuntu.com/
System information as of Thu Jun 18 11:19:13 CST 2015
System load: 0.0 Processes: 78
Usage of /: 68.8% of 19.56GB Users logged in: 0
Memory usage: 21% IP address for eth0: 104.131.134.14
Swap usage: 2%
Graph this data and manage this system at: https://landscape.canonical.com/
142 packages can be updated.
68 updates are security updates.
*** System restart required ***
Last login: Thu Jun 18 11:19:17 2015 from 222.129.101.44
-bash: /usr/local/bin/virtualenvwrapper.sh: No such file or directory
heamon7@heamon7:~$ abc=[location[i * (len + 1) + 1: (i + 1) * (len + 1) + 1] for i in range(rem)]
-bash: syntax error near unexpected token `('
heamon7@heamon7:~$ python
Python 2.7.6 (default, Mar 22 2014, 22:59:56)
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> location = 'location'
>>> len = 0
>>>
>>> rem = len(location)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'int' object is not callable
>>> rem = 8
>>> abc=[location[i * (len + 1) + 1: (i + 1) * (len + 1) + 1] for i in range(rem)]
>>> abc
['o', 'c', 'a', 't', 'i', 'o', 'n', '']
>>> len = 8
>>> abc=[location[i * (len + 1) + 1: (i + 1) * (len + 1) + 1] for i in range(rem)]
>>> abc
['ocation', '', '', '', '', '', '', '']
>>> len = 1
>>> abc=[location[i * (len + 1) + 1: (i + 1) * (len + 1) + 1] for i in range(rem)]
>>> abc
['oc', 'at', 'io', 'n', '', '', '', '']
>>> abc
好吧,我貌似也够无聊的了……
发自「贵邮」