【问题标题】:What is Ruby's equivalent to pythons xrange?Ruby 与 pythons xrange 的等价物是什么?
【发布时间】:2018-10-21 18:05:09
【问题描述】:

显然,python 中的xrangerange 快。因为xrange 懒惰地创建了一个对象序列。但是range 在内存中创建对象。

我想知道 Ruby 的 pythons xrange 等价物是什么?

【问题讨论】:

  • 我不了解 Ruby,但我搜索了“ruby 中的惰性范围”,结果似乎很有帮助。
  • 我自己搜索过这个,但我不能像在 python 中那样使用它。例如在 python 中:for i in xrange(m): matrix.append(map(int, raw_input().split()))@timgeb

标签: python ruby lazy-evaluation xrange


【解决方案1】:

Ruby 范围已经是惰性的,例如 Python 3 range。只需使用一个范围:

1..10  # includes endpoint
1...10 # excludes endpoint

不过,Ruby 中的惯用迭代通常不涉及范围。例如,如果你想做一件事n 次,就像在你的评论中一样:

n.times { do_something }

【讨论】:

  • 天哪,是这样吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-14
  • 2010-11-01
  • 2013-08-11
  • 1970-01-01
相关资源
最近更新 更多