.net中的一些数组排列问题,高手请进

豬哥 2014-12-11 11:38:19
我遇到这样的一个问题。数组的个数不固定。假如说 我有5个数组
我要从这5个数组中挑选出2个进行匹配,显示出匹配的类容并得到最后的结果

list1{张三,李四,王五}
list2{小牛,小马,小羊}
list3{昆虫,鸟类,人类}
list4{狼,马,羊,驴,狗,燕子,乌鸦,凤凰}
list5{头发,指甲,鼻子,嘴巴,眼睛}

这里是五个数组,我想怎么进行匹配,不要被文字迷惑,跟文字没有关系.我简单的匹配一下

arrlist{张三&小牛,张三&小马,张三&小羊,张三&昆虫,张三&鸟类,张三&人类,张三&狼,。。。。}


请问这样匹配该如何匹配呢?求高手指点!
...全文
461 36 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
36 条回复
切换为时间正序
请发表友善的回复…
发表回复
豬哥 2014-12-15
  • 打赏
  • 举报
回复
引用 35 楼 caozhy 的回复:
第一步的结果过滤下就可以了。
没有搞懂在哪里过滤,如果是字符过滤的话,可能会出现问题,万一 list1里面有list3里面的字符 ,这样就麻烦了、 大神,你说的第一步过滤是怎么过滤?
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
引用 11 楼 xl425107 的回复:
[quote=引用 3 楼 caozhy 的回复:]
var query = from x in list1
from y in list2.Concat(list3).Concat(list4).Concat(list5)
select x + "&" + y;
foreach (string item in query) Console.WriteLine(item);
这个方法只能查询到第一个数组跟第二个数组的排列 list1 & list2[/quote] 怎么可能,当然包括list3 ist4 list5
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 3 楼 caozhy 的回复:
var query = from x in list1
from y in list2.Concat(list3).Concat(list4).Concat(list5)
select x + "&" + y;
foreach (string item in query) Console.WriteLine(item);
这个方法只能查询到第一个数组跟第二个数组的排列 list1 & list2
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 9 楼 ee_2499021096 的回复:
按条件检索数组中的匹配元素 private string[] G_str_array;//定义字符串数组字段 //显示数组字段 private void Frm_Main_Load(object sender, EventArgs e) { G_str_array = new string[] {//为字符串数组字段赋值 "张小三一班","王小五二班","李中立三班","席桂璐四班"}; for (int i = 0; i < G_str_array.Length; i++)//循环输出字符串 { lab_Message.Text += G_str_array[i] + "\n"; } } //匹配字符串 private void txt_find_TextChanged(object sender, EventArgs e) { if (txt_find.Text != string.Empty)//判断查找字符串是否为空 { string[] P_str_temp = Array.FindAll//使用FindAll方法查找相应字符串 (G_str_array, (s) => s.Contains(txt_find.Text)); if (P_str_temp.Length > 0)//判断是否查找到相应字符串 { txt_display.Clear();//清空控件中的字符串 foreach (string s in P_str_temp)//向控件中添加字符串 { txt_display.Text += s + Environment.NewLine; } } else { txt_display.Clear();//清空控件中的字符串 txt_display.Text = "没有找到记录";//提示没有找到记录 } } else { txt_display.Clear();//清空控件中的字符串 } }
不好意思,你这个我不知道是干啥的啊,搜索,筛选的?可是我的这个不是筛选,是排列的问题
ee_2499021096 2014-12-12
  • 打赏
  • 举报
回复
按条件检索数组中的匹配元素 private string[] G_str_array;//定义字符串数组字段 //显示数组字段 private void Frm_Main_Load(object sender, EventArgs e) { G_str_array = new string[] {//为字符串数组字段赋值 "张小三一班","王小五二班","李中立三班","席桂璐四班"}; for (int i = 0; i < G_str_array.Length; i++)//循环输出字符串 { lab_Message.Text += G_str_array[i] + "\n"; } } //匹配字符串 private void txt_find_TextChanged(object sender, EventArgs e) { if (txt_find.Text != string.Empty)//判断查找字符串是否为空 { string[] P_str_temp = Array.FindAll//使用FindAll方法查找相应字符串 (G_str_array, (s) => s.Contains(txt_find.Text)); if (P_str_temp.Length > 0)//判断是否查找到相应字符串 { txt_display.Clear();//清空控件中的字符串 foreach (string s in P_str_temp)//向控件中添加字符串 { txt_display.Text += s + Environment.NewLine; } } else { txt_display.Clear();//清空控件中的字符串 txt_display.Text = "没有找到记录";//提示没有找到记录 } } else { txt_display.Clear();//清空控件中的字符串 } }
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
第一步的结果过滤下就可以了。
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 31 楼 caozhy 的回复:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<string>> lists = new List<List<string>>()
            {
                new List<string>() { "张三", "李四" },
                new List<string>() { "你", "我" },
                new List<string>() { "人" },
                new List<string>() { "食物", "材料" },
                new List<string>() { "x", "y" },
                new List<string>() { "d", "s" }
            };
            List<IEnumerable<List<string>>> query = SelectNFromM(lists, 3);
            foreach (IEnumerable<List<string>> item in query)
            {
                IEnumerable<IEnumerable<string>> query1 = CartesianProduct<string>(item.ToList());
                foreach (var item1 in query1)
                {
                    Console.WriteLine(string.Join("&", item1));
                }
            }
        }

        static List<IEnumerable<T>> SelectNFromM<T>(List<T> data, int n)
        {
            int[] are = Enumerable.Range(0, data.Count).ToArray();
            var result = are.Select(x => new int[] { x });
            for (int i = 0; i < n - 1; i++)
            {
                result = result.SelectMany(x => are.Where(y => y.CompareTo(x.First()) < 0).Select(y => new int[] { y }.Concat(x).ToArray()));
            }
            return result.Select(x => x.Select(y => data[y])).ToList();
        }

        static IEnumerable<IEnumerable<T>> CartesianProduct<T>(List<List<T>> source)
        {
            List<T[]> result = source[0].Select(x => new T[] { x }).ToList();
            for (int i = 1; i < source.Count; i++)
            {
                if (i == source.Count) break;
                result = result.SelectMany(x => source[i].Select(y => x.Concat(new T[] { y }).ToArray())).ToList();
            }
            return result;
        }
    }
}
大神 如果我固定 几个list必须显示,怎么整呢? 例子还是哪个例子 加入 我固定了 list1 必须出现,怎么整? l
豬哥 2014-12-12
  • 打赏
  • 举报
回复
谢谢 太谢谢了,得到解决!
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
张三&你&人 张三&我&人 李四&你&人 李四&我&人 张三&你&食物 张三&你&材料 张三&我&食物 张三&我&材料 李四&你&食物 李四&你&材料 李四&我&食物 李四&我&材料 张三&人&食物 张三&人&材料 李四&人&食物 李四&人&材料 你&人&食物 你&人&材料 我&人&食物 我&人&材料 张三&你&x 张三&你&y 张三&我&x 张三&我&y 李四&你&x 李四&你&y 李四&我&x 李四&我&y 张三&人&x 张三&人&y 李四&人&x 李四&人&y 你&人&x 你&人&y 我&人&x 我&人&y 张三&食物&x 张三&食物&y 张三&材料&x 张三&材料&y 李四&食物&x 李四&食物&y 李四&材料&x 李四&材料&y 你&食物&x 你&食物&y 你&材料&x 你&材料&y 我&食物&x 我&食物&y 我&材料&x 我&材料&y 人&食物&x 人&食物&y 人&材料&x 人&材料&y 张三&你&d 张三&你&s 张三&我&d 张三&我&s 李四&你&d 李四&你&s 李四&我&d 李四&我&s 张三&人&d 张三&人&s 李四&人&d 李四&人&s 你&人&d 你&人&s 我&人&d 我&人&s 张三&食物&d 张三&食物&s 张三&材料&d 张三&材料&s 李四&食物&d 李四&食物&s 李四&材料&d 李四&材料&s 你&食物&d 你&食物&s 你&材料&d 你&材料&s 我&食物&d 我&食物&s 我&材料&d 我&材料&s 人&食物&d 人&食物&s 人&材料&d 人&材料&s 张三&x&d 张三&x&s 张三&y&d 张三&y&s 李四&x&d 李四&x&s 李四&y&d 李四&y&s 你&x&d 你&x&s 你&y&d 你&y&s 我&x&d 我&x&s 我&y&d 我&y&s 人&x&d 人&x&s 人&y&d 人&y&s 食物&x&d 食物&x&s 食物&y&d 食物&y&s 材料&x&d 材料&x&s 材料&y&d 材料&y&s Press any key to continue . . .
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<List<string>> lists = new List<List<string>>()
            {
                new List<string>() { "张三", "李四" },
                new List<string>() { "你", "我" },
                new List<string>() { "人" },
                new List<string>() { "食物", "材料" },
                new List<string>() { "x", "y" },
                new List<string>() { "d", "s" }
            };
            List<IEnumerable<List<string>>> query = SelectNFromM(lists, 3);
            foreach (IEnumerable<List<string>> item in query)
            {
                IEnumerable<IEnumerable<string>> query1 = CartesianProduct<string>(item.ToList());
                foreach (var item1 in query1)
                {
                    Console.WriteLine(string.Join("&", item1));
                }
            }
        }

        static List<IEnumerable<T>> SelectNFromM<T>(List<T> data, int n)
        {
            int[] are = Enumerable.Range(0, data.Count).ToArray();
            var result = are.Select(x => new int[] { x });
            for (int i = 0; i < n - 1; i++)
            {
                result = result.SelectMany(x => are.Where(y => y.CompareTo(x.First()) < 0).Select(y => new int[] { y }.Concat(x).ToArray()));
            }
            return result.Select(x => x.Select(y => data[y])).ToList();
        }

        static IEnumerable<IEnumerable<T>> CartesianProduct<T>(List<List<T>> source)
        {
            List<T[]> result = source[0].Select(x => new T[] { x }).ToList();
            for (int i = 1; i < source.Count; i++)
            {
                if (i == source.Count) break;
                result = result.SelectMany(x => source[i].Select(y => x.Concat(new T[] { y }).ToArray())).ToList();
            }
            return result;
        }
    }
}
land_L 2014-12-12
  • 打赏
  • 举报
回复
百度百科里的笛卡尔乘积,竟然有C#源码。。。 程序使用说明 (1)将每个维度的集合的元素视为List<string>,多个集合构成List<List<string>> dimvalue作为输入 (2)将多维笛卡尔乘积的结果放到List<string> result之中作为输出 (3)int layer, string curstring只是两个中间过程的参数携带变量 (4)程序采用递归调用,起始调用示例如下: List<string> result = new List<string>(); Descartes.run(dimvalue, result, 0, ""); 即可获得多维笛卡尔乘积的结果。 PS:代码没测试过,仅供参考

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Linq;
public class Descartes
{
public static void run(List<List<string>> dimvalue, List<string> result, int layer, string curstring)
{
if (layer < dimvalue.Count - 1)
{
if (dimvalue[layer].Count == 0)
run(dimvalue, result, layer + 1, curstring);
else
{
for (int i = 0; i < dimvalue[layer].Count; i++)
{
StringBuilder s1 = new StringBuilder();
s1.Append(curstring);
s1.Append(dimvalue[layer][i]);
run(dimvalue, result, layer + 1, s1.ToString());
}
}
}
else if (layer == dimvalue.Count - 1)
{
if (dimvalue[layer].Count == 0) result.Add(curstring);
else
{
for (int i = 0; i < dimvalue[layer].Count; i++)
{
result.Add(curstring + dimvalue[layer][i]);
}
}
}
}
}
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 27 楼 caozhy 的回复:
[quote=引用 26 楼 xl425107 的回复:] [quote=引用 23 楼 caozhy 的回复:] [quote=引用 22 楼 xl425107 的回复:] [quote=引用 21 楼 caozhy 的回复:] 如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
不是啊,你理解错误了,我不是做彩票发财的,购买彩票要计算注数,买10场比赛,选择过关方式 N串N,然后计算出一共多少注,多少场,并不是说做BUG或者那种投机者的白日梦啊![/quote] 不知道你的M"串"N的串是什么意思。我的意思是,你得负责把你们彩票里面的术语翻译成我们能理解的意思。 如果是M选N,参考我之前贴出的链接里的代码。[/quote] 当然了,可能不是选择四个,有可能是五个 有可能是6个,这个数字不固定,我可以定义为 M 意思就是 N串M N是集合的个数 M是串的场次数,这个可能相对复杂点! [/quote] 只要你能把你要做什么说清楚,就不复杂。问题就是你自己说不清楚。而且你也不能用数学方法或者测试用例来精确描述你的问题。[/quote] 其实我也不知道咋描述,我之前一直不玩彩票的,最近在这个公司上班,现在要解析这些东西,我也只能求助大神们了!
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 25 楼 caozhy 的回复:
是不是对list先做m选n,然后求所选集合的笛卡尔积?
是的是的。就是那个意思。
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
引用 26 楼 xl425107 的回复:
[quote=引用 23 楼 caozhy 的回复:] [quote=引用 22 楼 xl425107 的回复:] [quote=引用 21 楼 caozhy 的回复:] 如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
不是啊,你理解错误了,我不是做彩票发财的,购买彩票要计算注数,买10场比赛,选择过关方式 N串N,然后计算出一共多少注,多少场,并不是说做BUG或者那种投机者的白日梦啊![/quote] 不知道你的M"串"N的串是什么意思。我的意思是,你得负责把你们彩票里面的术语翻译成我们能理解的意思。 如果是M选N,参考我之前贴出的链接里的代码。[/quote] 当然了,可能不是选择四个,有可能是五个 有可能是6个,这个数字不固定,我可以定义为 M 意思就是 N串M N是集合的个数 M是串的场次数,这个可能相对复杂点! [/quote] 只要你能把你要做什么说清楚,就不复杂。问题就是你自己说不清楚。而且你也不能用数学方法或者测试用例来精确描述你的问题。
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 23 楼 caozhy 的回复:
[quote=引用 22 楼 xl425107 的回复:] [quote=引用 21 楼 caozhy 的回复:] 如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
不是啊,你理解错误了,我不是做彩票发财的,购买彩票要计算注数,买10场比赛,选择过关方式 N串N,然后计算出一共多少注,多少场,并不是说做BUG或者那种投机者的白日梦啊![/quote] 不知道你的M"串"N的串是什么意思。我的意思是,你得负责把你们彩票里面的术语翻译成我们能理解的意思。 如果是M选N,参考我之前贴出的链接里的代码。[/quote] 当然了,可能不是选择四个,有可能是五个 有可能是6个,这个数字不固定,我可以定义为 M 意思就是 N串M N是集合的个数 M是串的场次数,这个可能相对复杂点!
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
是不是对list先做m选n,然后求所选集合的笛卡尔积?
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 23 楼 caozhy 的回复:
[quote=引用 22 楼 xl425107 的回复:] [quote=引用 21 楼 caozhy 的回复:] 如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
不是啊,你理解错误了,我不是做彩票发财的,购买彩票要计算注数,买10场比赛,选择过关方式 N串N,然后计算出一共多少注,多少场,并不是说做BUG或者那种投机者的白日梦啊![/quote] 不知道你的M"串"N的串是什么意思。我的意思是,你得负责把你们彩票里面的术语翻译成我们能理解的意思。 如果是M选N,参考我之前贴出的链接里的代码。[/quote] OK 好的,我重新细说一次,现在呢 用户选择出一些集合 list 1 list2 list3 list4 list5 当然 可能不止这么写list 所以我把这么list的count数称为 N 然后用户又选择了 要从N中选择4个串在一起 这个意思就是比如说 list1 {张三,李四}; list2{你,我};list3{人};list4{食物,材料};list5{x,y} .....listN{d,s}; 那么所谓的串就是 张三&你&人&食物 张三&我&人&材料 张三&你&人&材料 张三&我&人&食物 李四&你&人&食物。。。。 张三&你&人&x 李四 &我&人&x 就是要找这么些东西,找出匹配结果! 这就是所谓的串。 不知道大神能否指一条明路!
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
引用 22 楼 xl425107 的回复:
[quote=引用 21 楼 caozhy 的回复:] 如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
不是啊,你理解错误了,我不是做彩票发财的,购买彩票要计算注数,买10场比赛,选择过关方式 N串N,然后计算出一共多少注,多少场,并不是说做BUG或者那种投机者的白日梦啊![/quote] 不知道你的M"串"N的串是什么意思。我的意思是,你得负责把你们彩票里面的术语翻译成我们能理解的意思。 如果是M选N,参考我之前贴出的链接里的代码。
豬哥 2014-12-12
  • 打赏
  • 举报
回复
引用 21 楼 caozhy 的回复:
如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
不是啊,你理解错误了,我不是做彩票发财的,购买彩票要计算注数,买10场比赛,选择过关方式 N串N,然后计算出一共多少注,多少场,并不是说做BUG或者那种投机者的白日梦啊!
threenewbee 2014-12-12
  • 打赏
  • 举报
回复
如果彩票的规律真的能“算出来”,你想这个世界上有多少人比你会写程序?还轮得到你发财么?
加载更多回复(16)

111,101

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧