测试整数列表是否为奇数或偶数 [英] Testing if a list of integer is odd or even

查看:49
本文介绍了测试整数列表是否为奇数或偶数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图确定我的整数列表是由奇数还是偶数组成,我想要的输出是一个真和/或假的列表.我可以首先在列表上执行以下操作,还是需要创建循环?A是输出.

Trying to determine if my list of integer is made of odd or even numbers, my desired output is a list of true an/or false. Can I perform the following operation on the list lst or do I need to create a loop? A is the output.

    List <int> lst = new List <int>();
    A = IsOdd(lst);

推荐答案

您可以尝试使用Linq投影列表:

You could try using Linq to project the list:

var output = lst.Select(x => x % 2 == 0).ToList();

这将返回一个新的bool列表,以使 {1、2、3、4、5} 映射到 {false,true,false,true,false} .

This will return a new list of bools such that {1, 2, 3, 4, 5} will map to {false, true, false, true, false}.

这篇关于测试整数列表是否为奇数或偶数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆