c# - 可枚举负范围

标签 c# python range

在 python 中,我们有一个范围,也可以为具有负整数的数组生成。

例如:

In[4]: range(-2, 2+1)
Out[4]: [-2, -1, 0, 1, 2]

C# 中是否有等效的系统?我知道 IEnumerable 方法,但在尝试时我得到以下输出:

//Rextester.Program.Main is the entry point for your code. Don't change it.
//Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace Rextester
{
    public class Program
    {
        public static void Main(string[] args)
        {
            //Your code goes here
            Console.WriteLine("Hello, world!");
            int half_window = 2;
            var mySeq1 = Enumerable.Range(-2, 2+1).ToArray();
            foreach(var item in mySeq1)
    Console.Write(item.ToString() + " ");
        } 
    }
}

产生输出:

Hello, world!
-2 -1 0

是否已经有可用于获取 python 输出的内置方法?

最佳答案

Enumerable.Range 的第二个参数指定要生成的元素的计数,而不是停止值。

Console.WriteLine("Hello, world!");
int half_window = 2;
var mySeq1 = Enumerable.Range(-2, 5).ToArray();
//                                ^
foreach(var item in mySeq1)
    Console.Write(item.ToString() + " ");

输出:

Hello, world!
-2 -1 0 1 2 

关于c# - 可枚举负范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36287708/

相关文章:

c# - 如何安装和使用 Code Contracts?

c# - 如何从 linq 查询中提取特定值?

python - 抓取链接和标题 - 使用 beautifulsoup 存储在字典中

python - 在 Pydev 设置断点,但仅在回调上未命中断点

Jenkins BUILD_NUMBER 限制 - 最大构建号

javascript - 选择跨越多个 div 问题

c# - WPF:单击按钮打开新窗口

javascript - 突出显示 jQuery 日期选择器中的特定日期

Python执行目录和子目录中的所有Python文件

python - 将 x Axis 分成 2 个以上的部分 (Python)