简体   繁体   中英

Pair values from two lists

I have two lists with me as per the following example.

List<string> words = new List<string>() {"V","H","M" };

List<int> numbers = new List<int>() {10,20,30 };

I need to pair the values of these two lists so that my output needs to be exactly like the following text.

Desired output : V10 H20 M30

Try using Zip :

var result = words
  .Zip(numbers, (w, n) => $"{w}{n}");

Console.Write(string.Join(" ", result));

You could use Zip method for that.

You can try the following:

String.Join(" ", words.Zip(numbers, (first, second) => first + second))

我参加聚会有点晚了,但是这是一种不用Zip的简单方法:(x =项目,y =索引)

var mergedList = words.Select((x, y) => $"{x}{numbers.ElementAt(y)}");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM