ジップの.NETバージョンはしませんPythonのように任意の数の配列を扱うことができます。あなたは二回郵便番号を呼び出す必要があります:
Dim first As String() = { "a", "b", "c" }
Dim second As Integer() = { 1, 2, 3 }
Dim third As String() = { "x", "y", "z" }
Dim query = first.Zip(second, Function(f, s) New With { .First = f, .Second = s }) _
.Zip(third, Function(o, t) New With { o.First, o.Second, .Third = t })
For Each item in query
Console.WriteLine("{0}, {1}, {2}", item.First, item.Second, item.Third)
Next
別のオプションは、インデックスを含み、オーバーロードEnumerable.Select
methodを使用することです。このアプローチでは、作業しているタイプに依存してインデックスによるアクセスを許可します。パフォーマンス目的でインデックスアクセスをElementAt
メソッドに置き換えることはお勧めしません。また、この方法では、すべてのコレクションの長さが同じであるとみなします。そうでない場合は、例外がスローされます。これは次のように動作します:
Dim query2 = first.Select(Function(f, i) New With { .First = f, .Second = second(i), .Third = third(i) })
EDIT:一つの考えが直接のPythonを活用し、VB.NETからそれを呼び出すことです。私はこれがどのように処理されるのか本当にわからないし、それをすべて設定する学習曲線があるだろう。そのトピックの詳細については、「call python from c#」または「vb.net」を検索してください。
匿名タイプを動的に作成することはできません。私が思いついた最も近いアプローチは、.NET 4.0のExpandoObject
を使用することです。 VB.NETでC#のdynamic
キーワードを使用するには、実際にobject
の下にあるDim o = 5
のような型を指定せずにオブジェクトを初期化できるはずです。これを達成するには、おそらくOption Infer On
とOption Strict Off
を設定する必要があります。
次のコードでは、配列を入力として想定しています。残念ながら、Count
にアクセスしようとすると、ダイナミックタイプとその他のIEnumerable<T>
を混在させるのは難しくなります。 Jon Skeetに関連記事があります:Gotchas in dynamic typing。そのため私は配列についていました。 Count
プロパティを使用するにはList<T>
に変更できますが、多くの作業をしなくても間違いなく混在することはありません。 (興味のある人のための)
VB.NET
Dim first As String() = { "a", "b", "c" }
Dim second As Integer() = { 1, 2, 3 }
Dim third As String() = { "x", "y", "z" }
Dim fourth As Boolean() = { true, false, true }
Dim list As New List(Of Object) From { first, second, third, fourth }
' ensure the arrays all have the same length '
Dim isValidLength = list.All(Function(c) c.Length = list(0).Length)
If isValidLength
Dim result As New List(Of ExpandoObject)()
For i As Integer = 0 To list(i).Length - 1
Dim temp As New ExpandoObject()
For j As Integer = 0 To list.Count - 1
CType(temp, IDictionary(Of string, Object)).Add("Property" + j.ToString(), list(j)(i))
Next
result.Add(temp)
Next
' loop over as IDictionary '
For Each o As ExpandoObject In result
For Each p in CType(o, IDictionary(Of string, Object))
Console.WriteLine("{0} : {1}", p.Key, p.Value)
Next
Console.WriteLine()
Next
' or access via property '
For Each o As Object In result
Console.WriteLine(o.Property0)
Console.WriteLine(o.Property1)
Console.WriteLine(o.Property2)
Console.WriteLine(o.Property3)
Console.WriteLine()
Next
End If
C#の同等
string[] first = { "a", "b", "c" };
int[] second = { 1, 2, 3 };
string[] third = { "x", "y", "z" };
bool[] fourth = { true, false, true };
var list = new List<dynamic> { first, second, third, fourth };
bool isValidLength = list.All(l => l.Length == list[0].Length);
if (isValidLength)
{
var result = new List<ExpandoObject>();
for (int i = 0; i < list[i].Length; i++)
{
dynamic temp = new ExpandoObject();
for (int j = 0; j < list.Count; j++)
{
((IDictionary<string, object>)temp).Add("Property" + j, list[j][i]);
}
result.Add(temp);
}
// loop over as IDictionary
foreach (ExpandoObject o in result)
{
foreach (var p in (IDictionary<string, object>)o)
Console.WriteLine("{0} : {1}", p.Key, p.Value);
Console.WriteLine();
}
// or access property via dynamic
foreach (dynamic o in result)
{
Console.WriteLine(o.Property0);
Console.WriteLine(o.Property1);
Console.WriteLine(o.Property2);
Console.WriteLine(o.Property3);
Console.WriteLine();
}
}
現在、新しいIEnumerableを作成するためのループでの歩留まり。私はまだネストされた郵便番号で行うことが可能かどうかを見たいと思っています。 – user544111