我的目标很简单,我试图为数据库中的产品生成所有可能组合的列表。
例如,产品选项如下
我希望能够自动生成所有3种组合的每一个组合:
Small, Red, Mens
Small, Green, Mens
Small, Blue, Mens
etc无论将2、3、4或5个数组传递到其中,我都需要该函数来工作。
我做了相当多的研究,并遇到了以下文章,但未能完成我的目标。
我发现的文章如下:
发布于 2014-02-17 22:24:52
在笛卡尔产品上修改埃里克·利珀特的博客的代码:
Private Function CartesianProduct(Of T)(ParamArray sequences As T()()) As T()()
' base case:
Dim result As IEnumerable(Of T()) = {New T() {}}
For Each sequence As var In sequences
Dim s = sequence
' don't close over the loop variable
' recursive case: use SelectMany to build the new product out of the old one
result = From seq In result
From item In s
Select seq.Concat({item}).ToArray()
Next
Return result.ToArray()
End Function用法:
Dim s1 As String() = New String() {"small", "med", "large", "XL"}
Dim s2 As String() = New String() {"red", "green", "blue"}
Dim s3 As String() = New String() {"Men", "Women"}
Dim ss As String()() = CartesianProduct(s1, s2, s3)发布于 2014-02-17 22:30:21
三个循环--一个在另一个里面--应该能起作用。
所以伪代码..。
For each value in sex array
For each value in size array
For each value in colour array
Output sex, size, colour values
Next colour
Next size
Next sex更新的Psudo
Sub ouputOptions(array1, array2, array3, array4, array5)
For each value in array1
For each value in array2
If array3 Not Nothing Then
For each value in array3
If array4 Not Nothing Then
For each value in array4
If array5 Not Nothing Then
For each value in array5
output array1, array2, array3, array4, array5 values
next array5
Else
Output array1, array2, array3, array4 values
End if
Next array4
Else
Output array1, array2, array3 values
End if
next array3
Else
Output array1, array2 values
End if
Next array2
Next array1
End Sub您需要将array3指定为5作为可选的
发布于 2014-02-17 23:14:24
您可以通过一些递归来实现这一点。
下面是返回字符串数组的结果。
Public class Permuter
Public Function Permute(ParamArray toPermute As String()()) As String()()
Return DoPermute(Nothing, toPermute)
End Function
''' <summary>
''' Permute the first two arrays,then pass that, and the remainder recursively
''' </summary>
Private Function DoPermute(working As String()(), toPermute As String()()) As String()()
Dim nextWorking As String()()
If working Is Nothing Then
'Make a new working list
nextWorking = (From a In toPermute(0)
Select {a}).ToArray
Else
'Combine from the next working list
nextWorking = (From a In working, b In toPermute(0)
Select a.Concat({b}).ToArray).ToArray
End If
If toPermute.Length > 1 Then
'Go Around again
Dim nextPermute = toPermute.Skip(1).ToArray
Return DoPermute(nextWorking, nextPermute)
Else
'We're done
Return nextWorking
End If
End Function
End Class将公共方法称为:
Dim permuter = New Permuter
Dim permutations = permuter.Permute({"a", "b", "c"}, {"1", "2", "3"}, {"x", "y", "z"})更新:使用@DStanley的Eric博客引用,下面是那个职位中提到的累加器方法的转换
Public Function CartesianProduct(Of T)(ParamArray sequences As T()()) As IEnumerable(Of IEnumerable(Of T))
Dim emptyProduct As IEnumerable(Of IEnumerable(Of T)) = {Enumerable.Empty(Of T)()}
Return sequences.Aggregate(
emptyProduct,
Function(accumulator, sequence) _
From accseq In accumulator, item In sequence
Select accseq.Concat({item})
)
End Function请注意,这将返回延迟查询,而不是一组扩展的数组。
https://stackoverflow.com/questions/21840430
复制相似问题