如何做到Python的拉链在C#中? [英] How to do Python's zip in C#?

查看:110
本文介绍了如何做到Python的拉链在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的拉链函数执行以下操作:

  A = [1,2,3]
B = [6,7,8]
压缩= ZIP(A,B)
 

结果

 [[1,6],[2,7],[3,8]
 

解决方案

如何<一个href="http://bartdesmet.net/blogs/bart/archive/2008/11/03/c-4-0-feature-focus-part-3-intermezzo-linq-s-new-zip-operator.aspx">this?

C#4.0 LINQ新的ZIP操作符

 公共静态的IEnumerable&LT; TResult&GT;邮编及LT; TFirst,TSecond,TResult&GT;(
        这IEnumerable的&LT; TFirst&GT;第一,
        IEnumerable的&LT; TSecond&GT;第二,
        FUNC&LT; TFirst,TSecond,TResult&GT; FUNC);
 

Python's zip function does the following:

a = [1, 2, 3]
b = [6, 7, 8]
zipped = zip(a, b)

result

[[1, 6], [2, 7], [3, 8]]

解决方案

How about this?

C# 4.0 LINQ'S NEW ZIP OPERATOR

public static IEnumerable<TResult> Zip<TFirst, TSecond, TResult>(
        this IEnumerable<TFirst> first,
        IEnumerable<TSecond> second,
        Func<TFirst, TSecond, TResult> func);

这篇关于如何做到Python的拉链在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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