Skip to main content

連番、シーケンス(sequence,range)

Python

py
nums = range(1, 4) # => [1, 2, 3]
py
nums = range(1, 5,  2)      # => [1, 3]
nums = range(5, 1, -1) # => [5, 4, 3, 2]

Swift

swift
nums = 1...3 // => 1, 2, 3 # Range (open range)
swift
nums = 1..<3 // => 1, 2  # ClosedRante

でもこれは厳密には「範囲」か

コレクション

swift
stride(from: 1, to: 3, by: 1) // => 1, 2   StrideTo 型
stride(from: 1, through: 3, by: 1) // => 1, 2, 3 StrideThrough 型

Bash

sh
arr1=`seq 1 3`

Ruby

rb
nums = (1..3).to_a  # => [1, 2, 3]
rb
nums = (1...5).to_a          # => [1, 2, 3, 4]     # 1 <= n <  5
nums = (1..5).step(2).to_a # => [1, 3, 5]

nums = (1..5).to_a.reverse # => [5, 4, 3, 2, 1]]

alphabets = ('a'..'z').to_a # => ['a', 'b', .., 'z']

1.upto(5).to_a
5.downto(1).to_a
1.step(10, 2).to_a

PowerShell

powershell
1..10