python – 在Ruby中逐步将数组拆分为子数组
发布时间:2020-09-25 11:13:25 所属栏目:Python 来源:互联网
导读:在 Python中,我可以使用“jump-step”对数组进行切片.例: In [1]: a = [1,2,3,4,5,6,7,8,9] In [4]: a[1:7:2] # start from index = 1 to index 7, with step = 2Out[4]: [2, 4, 6] Ruby能做到吗? a = [1,2,3,4,5,6,7,8,9]a.values_
|
在 Python中,我可以使用“jump-step”对数组进行切片.例: In [1]: a = [1,2,3,4,5,6,7,8,9] In [4]: a[1:7:2] # start from index = 1 to index < 7,with step = 2 Out[4]: [2,6] Ruby能做到吗? 解决方法a = [1,9] a.values_at(*(1...7).step(2)) - [nil] #=> [2,6] 虽然在上面的例子中 – [nil]部分不是必需的,但它只是用于你的范围超过数组的大小,否则你可能得到这样的东西: a = [1,9] a.values_at(*(1..23).step(2)) #=> [2,nil,nil] (编辑:长春站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- python – 使用numpy将alpha通道添加到RGB数组
- 什么是python的_random?
- 如何将intersphinx与特定于django的结构(如设置)相关联?
- python – 如何避免使用Google App Engine发送电子邮件时出
- 在Python 2.7.3中为numpy数组指定字段名称
- python-2.7 – 在Python 2.7中手动构建ConfigParser的深层副
- 用最恐怖的方式替换第一个和最后一个字符串
- python – numpy – 将非连续数据转换为适当的连续数据
- python – Matplotlib第二个带有转换值的x轴
- Python – 将非常大(6.4GB)的XML文件转换为JSON
推荐文章
站长推荐
- 使用Python库绘制共享相同y轴的两个水平条形图
- python – 将数字字符引用表示法转换为unicode字
- TypeError:’function’对象不可订阅 – Python
- python中的Doc,rtf和txt阅读器
- 为什么python设计为str(无)返回’None’而不是空
- python – ElementTree find()/ findall()找不到
- python:if __name__ == __main__: 的作用
- 为什么python-cgi在unicode上失败?
- 突出显示python docstrings作为注释(vim语法高亮
- 如何在python / pygame中制作按钮?
热点阅读
