简体   繁体   English

如何选择不同的列表 <T> 来自清单 <List<T> &gt;?

[英]How to select distinct List<T>'s from a List<List<T>>?

HI, I am working on a simple class to combine items of any type... this is for a poker game, this is how it looks: 嗨,我正在研究一个简单的类来组合任何类型的项目...这是用于扑克游戏,它的外观如下:

public static List<List<T>> combinar<T>(List<T> items, int take)
{
    List<List<T>> combs = new List<List<T>>();
    var stuff = permutar<T>(items, take);

    var all = from s in stuff
                select new Tuple<List<T>, string>(s, String.Join("", s.OrderBy(c => c).Select(c => c.ToString())));
    var strs = all.Select(s => s.Item2).Distinct();
    foreach (var str in strs)
    {
        combs.Add(all.First(a => a.Item2 == str).Item1);
    }
    return combs;
}
public static List<List<T>> permutar<T>(List<T> list, int take)
{
    List<List<T>> combs = new List<List<T>>();
    foreach (var item in list)
    {
        var newlist = list.Where(i => !i.Equals(item)).ToList();
        var returnlist = take <= 1 ? new List<List<T>> { new List<T>() } : permutar(newlist, take - 1);
        foreach (var l in returnlist)
        {
            l.Add(item);
        }
        combs.AddRange(returnlist);
    }

    return combs;
}

so permutation works perfect.. but I am having some trouble with combination, when T is Card it takes hell lots of time to complete... so my question is how to select distinct lists form the result of permutation??? 所以排列是完美的..但是我在组合时遇到了一些麻烦,当T为Card时,要花很多时间才能完成...所以我的问题是如何从排列的结果中选择不同的列表?

this is the Card Class: 这是卡类:

public class Card : IComparable
{
    Suite _Suite;
    public Suite Suite
    {
        get { return _Suite; }
        set { _Suite = value; }
    }
    Grade _Grade;
    public Grade Grade
    {
        get { return _Grade; }
        set { _Grade = value; }
    }
    string _symbol;
    public string Symbol
    {
       //stuff
    }
    public PictureBox Picture
    {
        //stuff
    }
    public override string ToString()
    {
        return _Grade.ToString() + " " + _Suite.ToString();
    }
    public int CompareTo(object obj)
    {
        Card card = (Card)obj;
        return card.Grade > this.Grade ? -1 : card.Grade < this.Grade ? 1 : 0;
    }
}

Assuming you don't want to make any big algorithmic changes, your biggest problem here is 假设您不想进行任何大的算法更改,那么这里最大的问题是

combs.Add(all.First().Item1);

That doesn't make any sense. 那没有任何意义。 Perhaps you meant 也许你是说

combs.Add(all.First(c => c.Item2 == str)).Item1);

However, that would be very slow; 但是,那将非常慢。 if that's what you want, you should put the results of all into a hash table keyed by the string, and use that instead of looping through Distinct results. 如果这是您想要的,则应将all结果放入以该字符串为键的哈希表中,并使用该结果而不是遍历Distinct结果。

If you wanted to get combinations without computing permutations first, the way to do it would be like this. 如果您想在不首先计算排列的情况下获得组合,则这样做的方式将是这样。 Given some objects, to find the combinations of length K: if K is 0, return the empty list. 给定一些对象,以查找长度为K的组合:如果K为0,则返回空列表。 Else, for each object, take that object, and then recursively append all the K-minus-1-length combinations of the rest of the objects. 否则,对于每个对象,获取该对象,然后递归地附加其余对象的所有K--1长度组合。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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