Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在数组中建立一串排列_C# - Fatal编程技术网

C# 在数组中建立一串排列

C# 在数组中建立一串排列,c#,C#,我有一个需要调用的api,传入一个值字符串。这些值的构造如下所示: string searchString = string.Join(" ", myList.ToArray()); // remove any numbers and return complete words MatcCollection strMatch = Regex.Matches(searchString, @"[^\W\d]+"); var values = strMatch.Cast<Group>()

我有一个需要调用的api,传入一个值字符串。这些值的构造如下所示:

string searchString = string.Join(" ", myList.ToArray());
// remove any numbers and return complete words
MatcCollection strMatch = Regex.Matches(searchString, @"[^\W\d]+");

var values = strMatch.Cast<Group>().Select(g => g.Value).ToArray();

var combinations = values.Permutations();
string searchString=string.Join(“,myList.ToArray());
//删除所有数字并返回完整的单词
MatcCollection strMatch=Regex.Matches(searchString,@“[^\W\d]+”;
var values=stratch.Cast().Select(g=>g.Value.ToArray();
变量组合=值。排列();
现在,我有了我需要的数组,我调用下面的排列方法:

public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> source)
    {
        if (source == null)
            throw new ArgumentException("source");
        return permutations(source.ToArray());
    }
公共静态IEnumerable置换(此IEnumerable源)
{
if(source==null)
抛出新的ArgumentException(“源”);
返回置换(source.ToArray());
}
排列方法是:

private static IEnumerable<IEnumerable<T>> permutations<T>(IEnumerable<T> source)
    {
        var c = source.Count();
        if (c == 1)
            yield return source;
        else
            for (int i = 0; i < c; i++)
                foreach (var p in permutations(source.Take(i).Concat(source.Skip(i + 1))))
                    yield return source.Skip(i).Take(1).Concat(p);
    }
私有静态IEnumerable置换(IEnumerable源)
{
var c=source.Count();
如果(c==1)
收益回报源;
其他的
对于(int i=0;i
使用7个项目的示例列表{1,2,3,4,5,6,7},这段代码返回了大量长度为7个元素的列表

我需要创建以下内容:

第一次迭代:

返回结果=1

第二次迭代 返回结果=1+“”+2

诸如此类


我从SO上的一篇文章中得到了上面的示例代码,所以我不知道如何正确地更改它以获得我所需要的。

那么我是否正确地认为,不仅您需要7项的所有排列,而且还需要枚举它们的任何子集(类似于所有组合)

我想获得这种行为的最简单方法是在
置换方法中添加某种长度参数:

private static IEnumerable<IEnumerable<T>> permutations<T>(IEnumerable<T> source, int length)
{
    var c = source.Count();
    if (length == 1 || c == 1)
        foreach(var x in source)
            yield return new T[] { x };
    else
        for (int i = 0; i < c; i++)
            foreach (var p in permutations(source.Take(i).Concat(source.Skip(i + 1)), length - 1))
                yield return source.Skip(i).Take(1).Concat(p);
}
私有静态IEnumerable置换(IEnumerable源,int-length)
{
var c=source.Count();
如果(长度==1 | | c==1)
foreach(源中的变量x)
产生返回新的T[]{x};
其他的
对于(int i=0;i
然后使用1到n的参数调用此方法:

public static IEnumerable<IEnumerable<T>> Permutations<T>(this IEnumerable<T> source)
{
    if (source == null)
        throw new ArgumentException("source");
    var src = source.ToArray();
    for (int i = 1; i <= src.Length; i++)
        foreach (var result in permutations(src, i))
            yield return result;
}
公共静态IEnumerable置换(此IEnumerable源)
{
if(source==null)
抛出新的ArgumentException(“源”);
var src=source.ToArray();

对于(int i=1;i您想要的是组合,而不是置换:)这是关于列表olydis主题的goto答案,可以是任何数字,7是我目前正在运行的示例。在我的回答中,我也只想解决这个示例-我的代码不希望它是7,是吗?;)这个olydis的两个错误:无法隐式转换类型“System.Collections.Generic.IEnumerable”到“System.Collections.Generic.IEnumerable”。存在显式转换(是否缺少强制转换?),并且无法将类型“System.Collections.Generic.IEnumerable”隐式转换为“T”。存在显式转换(是否缺少强制转换?),可以对该转换进行排序,但仍会抱怨返回新的T[]{source}很好的一个。是否可以为每个项目返回一个字符串。它当前是一个数组。例如value=one-value=one-two-value=one-two-three等