关于List TO Dictionary

shellhy_cn 2014-05-24 01:07:56
我想把一个List<string>(里面是这样的格式:218.173.0.0:218.213.255.255这样的IP段格式)中的数据转为Dictionary<int, int>
static Dictionary<int, int> ips = new int[][]
{
//就是把List中的数据放这里初始化
Enumerable.Range(607649792, 608174079 - 607649792 + 1).ToArray(),
Enumerable.Range(1038614528, 1039007743 - 1038614528 + 1).ToArray(),
Enumerable.Range(1783627776, 1784676351 - 1783627776 + 1).ToArray(),
}

请问怎么弄
...全文
367 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
shellhy_cn 2014-05-29
  • 打赏
  • 举报
回复
已经搞定了,用的LIST,然后随机,字典太大
  • 打赏
  • 举报
回复
引用 8 楼 shellhy_cn 的回复:
比如这样: "深圳"=>"222.14.0.0:222.17.255.255", "19.147.0.0:19.187.255.255", "107.19.0.0:107.20.255.255" "北京"=>"212.18.0.0:212.19.255.255", "20.147.0.0:20.187.255.255", "117.19.0.0:117.20.255.255"
如果你没有自己定义Type的概念,那就没有办法了。 如果有,那么你可以写(例如):
public static List<MyLib> lib1;
public static SortedSet<MyLib> Lib2;

public class MyLib
{
    public long From;
    public long To;
    public Area;
}
而不要只会抱着基本的数据。 但愿这对你来说不是对牛弹琴。
zhouxiulu 2014-05-24
  • 打赏
  • 举报
回复
你这样完全可以写到数据库中,这样的话就很方便了
sunny906 2014-05-24
  • 打赏
  • 举报
回复
引用 7 楼 shellhy_cn 的回复:
就是想根据地区的IP段来随机生成地区的IP地区
不太明白
sunny906 2014-05-24
  • 打赏
  • 举报
回复
引用 6 楼 shellhy_cn 的回复:
字典方法不行,IP段太大,程序就死掉了 请问这样的存储怎么表达: [1]=>{"2323", "23432", "344334", "435445"} [2]->{"1111", "2222", "3333"} ..... 这样就可以先随机取到[1],然后再随机取[1]里面的字符串
这样表达

        Dictionary<int,List<string>> list=new Dictionary<int,List<string>>();
        list.Add(1,new List<string>(){ "2323","23432","344334","435445" });
        list.Add(2,new List<string>(){ "1111","2222","3333" });
shellhy_cn 2014-05-24
  • 打赏
  • 举报
回复
比如这样: "深圳"=>"222.14.0.0:222.17.255.255", "19.147.0.0:19.187.255.255", "107.19.0.0:107.20.255.255" "北京"=>"212.18.0.0:212.19.255.255", "20.147.0.0:20.187.255.255", "117.19.0.0:117.20.255.255"
shellhy_cn 2014-05-24
  • 打赏
  • 举报
回复
就是想根据地区的IP段来随机生成地区的IP地区
shellhy_cn 2014-05-24
  • 打赏
  • 举报
回复
字典方法不行,IP段太大,程序就死掉了 请问这样的存储怎么表达: [1]=>{"2323", "23432", "344334", "435445"} [2]->{"1111", "2222", "3333"} ..... 这样就可以先随机取到[1],然后再随机取[1]里面的字符串
exception92 2014-05-24
  • 打赏
  • 举报
回复
以 “:” 分割成string[],在以“.”号分割成sting[] ,循环添加到字典中。
sunny906 2014-05-24
  • 打赏
  • 举报
回复

List<string> list = new List<string>() { "218.173.0.0:218.213.255.255" };
            Dictionary<string, List<string>> ipSet = new Dictionary<string, List<string>>();
            List<string> temp = new List<string>();
            foreach (string ipArea in list)
            {
                temp.Clear();
                string[] ips = ipArea.Split(':')[0].Split('.');
                string[] ipe = ipArea.Split(':')[1].Split('.');
                for (int m = int.Parse(ips[0]); m <= int.Parse(ipe[0]); m++)
                {
                    for (int n = int.Parse(ips[1]); n <= int.Parse(ipe[1]); n++)
                    {
                        for (int i = int.Parse(ips[2]); i <= int.Parse(ipe[2]); i++)
                        {
                            for (int j = int.Parse(ips[3]); j <= int.Parse(ipe[3]); j++)
                            {
                                temp.Add(string.Format("{0}:{1}:{2}:{3}", m, n, i, j));
                            }
                        }
                    }
                }
                ipSet.Add(ipArea, temp);
            }
shellhy_cn 2014-05-24
  • 打赏
  • 举报
回复
就放几个IP段在字典中
bwangel 2014-05-24
  • 打赏
  • 举报
回复
如果你想根据IP来区分地域的话,只需要记住一系列起始IP和结束IP两个关键值就行了。 ipv4虽然只有32位,但理论上也有40多亿个,你全放在字典里显然是不太现实。
shellhy_cn 2014-05-24
  • 打赏
  • 举报
回复
我是先从嵌入资源中读文本,文本中存放有IP段,然后读到List,后面生成字典

110,556

社区成员

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

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

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