同时排序(C#)?
我有一个单词频率列表,其中包含按字母顺序排列的字符串和表示单词频率的未排序的整数(不需要读取txt或其他导致用户在控制台中键入“(字母)(数字)”查询的内容)。我不需要计算它们或类似的东西,而是通过每个特定的输入来打印最频繁的单词,例如控制台中的查询:“AA12”。在本例中,它以“A”开头,因此理想的做法是检索最频繁的同时排序(C#)?,c#,algorithm,sorting,C#,Algorithm,Sorting,我有一个单词频率列表,其中包含按字母顺序排列的字符串和表示单词频率的未排序的整数(不需要读取txt或其他导致用户在控制台中键入“(字母)(数字)”查询的内容)。我不需要计算它们或类似的东西,而是通过每个特定的输入来打印最频繁的单词,例如控制台中的查询:“AA12”。在本例中,它以“A”开头,因此理想的做法是检索最频繁的开始时间(“A”),其中至少有5个单词按与其频率相关的降序排列,但同时按其A-Z顺序排列 我读过很多关于BST的东西,字典,元组,分类列表,列表,分类集,Linq。。。还有算法书籍
开始时间(“A”)
,其中至少有5个单词按与其频率相关的降序排列,但同时按其A-Z顺序排列
我读过很多关于BST的东西,字典,元组,分类列表,列表,分类集,Linq。。。还有算法书籍,我了解到键和值可以按升序、降序、A-Z排序,但不能同时排序。。。有人可以向我解释如何引入这个查询“aa12”,其中我已经拆分为字符串a=“AA”;int b=12;进入一个BST或字符串的二元搜索树,int单词频率样式,但不需要计数,只需应用一个查询,检索与字符串匹配的5个最频繁的单词,以及这个100000单词频率列表的int,并像谷歌搜索自动完成一样打印控制台,但更基本
示例词频A-Z列表:
AA 12
AAA 32
AAB 4
AABB 38
BBAA 3
CDDDA 76
...
YZZZ 45
ZZZZZY 356
用户查询:“AA 15”
理想答案:
AAA
AA
AABB
AAB
守则:
var list = new List<KeyValuePair<string, int>>();
StreamReader sr = new StreamReader("C:\\dicti.txt");
while (true)
{
string line = sr.ReadLine(); //read each line
string[] ln;
if (line == null) break; // no more lines
try
{
ln = line.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
string a = ln[0];
int b = Convert.ToInt32(ln[1]);
list.Add(new KeyValuePair<string, int>(a, b));
}
catch (IndexOutOfRangeException)
{
break;
}
string word = Console.ReadLine();
string[] ln2;
ln2 = word.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
string am = ln2[0];
int bm = Convert.ToInt32(ln2[1]);
var list=newlist();
StreamReader sr=新的StreamReader(“C:\\dict.txt”);
while(true)
{
string line=sr.ReadLine();//读取每一行
字符串[]ln;
if(line==null)break;//不再有行
尝试
{
ln=line.Split(默认值(字符串[]),StringSplitOptions.RemoveEmptyEntries);
字符串a=ln[0];
intb=转换为32(ln[1]);
添加(新的KeyValuePair(a,b));
}
捕获(IndexOutOfRangeException)
{
打破
}
string word=Console.ReadLine();
字符串[]ln2;
ln2=word.Split(默认值(字符串[]),StringSplitOptions.RemoveEmptyEntries);
字符串am=ln2[0];
int bm=Convert.ToInt32(ln2[1]);
这是我到目前为止所写的代码。我对如何获得与用户查询的第一个字母对应的按字母顺序和频率排序的值有些迷茫
这是我的实际版本的代码…我有1:15分钟的时间阅读完整的1000字频率列表,所以…我现在想知道如何改进我的lambdas以达到15秒1000字频率列表的要求,或者如果lambdas无法工作,我该怎么办
static void Main(string[] args)
{
var dic = new Dictionary<string, int>();
int contador = 0;
StreamReader sr = new StreamReader("C:\\dicti.txt");
while (true)
{
string line = sr.ReadLine(); // To read lines
string[] ln;
if (line == null) break; // There is no more lines
try
{
ln = line.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
string a = ln[0];
int b = Convert.ToInt32(ln[1]);
dic.Add(a,b);
}
catch (IndexOutOfRangeException) { break; }
}
string[] ln2;
string am,word;
int bm;
do
{
//counter++;
do
{
word = Console.ReadLine();
ln2 = word.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries);
am = ln2[0];
bm = Convert.ToInt32(ln2[1]);
} while (!(am.Length >= 2 && bm >= 1 && bm <= 1000000 ));
if (true)
{
var aj = (dic.Where(x => x.Value >= bm).Where(x => x.Key.StartsWith(am)).OrderByDescending(d => d.Value).Take(2));
foreach (var p in aj)
{
Console.WriteLine("{0} ", p.Key);
}
}
} while (counter < 1001);
}
}
static void Main(字符串[]args)
{
var dic=新字典();
int contador=0;
StreamReader sr=新的StreamReader(“C:\\dict.txt”);
while(true)
{
string line=sr.ReadLine();//读取行
字符串[]ln;
if(line==null)break;//不再有行
尝试
{
ln=line.Split(默认值(字符串[]),StringSplitOptions.RemoveEmptyEntries);
字符串a=ln[0];
intb=转换为32(ln[1]);
dic.添加(a,b);
}
catch(IndexOutOfRangeException){break;}
}
字符串[]ln2;
字符串am,字;
int bm;
做
{
//计数器++;
做
{
word=Console.ReadLine();
ln2=word.Split(默认值(字符串[]),StringSplitOptions.RemoveEmptyEntries);
am=ln2[0];
bm=转换为32(ln2[1]);
}while(!(am.Length>=2&&bm>=1&&bm x.Value>=bm).Where(x=>x.Key.StartsWith(am)).OrderByDescending(d=>d.Value).Take(2));
foreach(aj中的var p)
{
Console.WriteLine(“{0}”,p.Key);
}
}
}而(计数器<1001);
}
}
}你想要这样的东西吗
public static IEnumerable<KeyValuePair<string, int>> SearchAndSortBy(Dictionary<string, int> fullSet, string searchFilter)
{
return fullSet.Where((pair) => pair.Key.Contains(searchFilter)).OrderByDescending((pair) => pair.Value);
}
我甚至可以将for循环更改为:
foreach (var item in results.Take(5))
如果我只想要前5名的话。我想你可以调整
OrderBy
来满足你的搜索需求。让我们快速浏览一下:
你的意见:
AA 12
AAA 32
AAB 4
AABB 38
BBAA 3
CDDDA 76
搜索“AA”所需的结果
所以AAA
在AA
之前,因为它的频率更高,而AABB
在后面,因为AABB
。现在问题来了:它也是AA
,因此如果按字母顺序排序,那么AA
将始终出现在AAA
之前,而不管其频率如何
但是,如果您用最后一个字符“继续”每个单词,那么您可以通过先按字母顺序排序,然后按频率排序得到所需的内容:
public static IEnumerable<KeyValuePair<string, int>> FilterAndSort(IEnumerable<KeyValuePair<string, int>> fullSet, string searchFilter, int maxKeyLength)
{
return fullSet
.Where(p => p.Key.StartsWith(searchFilter))
.OrderBy(p => p.Key.PadRight(maxKeyLength, p.Key.Last()))
.ThenByDescending(p => p.Value);
}
您可以在阅读列表时通过预计算填充词来优化它。在这种情况下,您可能需要使用
元组(原始单词、填充单词、频率)。而不是KeyValuePair
会占用更多的内存,但您只需在每个过滤器上执行一次即可。不清楚“同时排序”的含义以及您的要求。Sort()对列表进行排序,如果这是您要查找的。如果没有,请解释输入应该如何产生输出。通过“同步排序”,我想知道是否有一种方法可以对100000个单词的频率列表进行排序,不仅可以按一种排序,还可以按降序和从a-Z同时排序。list.sort()的问题它只关注一个标准,正如我在上面已经提到的,我觉得它不适合这个项目的目的。一个程序,需要从10万个单词频率列表中按字母顺序排序,只进行搜索,即用户类型控制台查询:“AA 223”。检索按字母顺序和降序排列的4-5个最常见单词。我仍然不懂。是否要将列表的两个副本排序为两个
AA 12
AAA 32
AAB 4
AABB 38
BBAA 3
CDDDA 76
AAA
AA
AABB
AAB
public static IEnumerable<KeyValuePair<string, int>> FilterAndSort(IEnumerable<KeyValuePair<string, int>> fullSet, string searchFilter, int maxKeyLength)
{
return fullSet
.Where(p => p.Key.StartsWith(searchFilter))
.OrderBy(p => p.Key.PadRight(maxKeyLength, p.Key.Last()))
.ThenByDescending(p => p.Value);
}
List<KeyValuePair<string, int>> list = new List<KeyValuePair<string,int>>
{
new KeyValuePair<string, int>("AA", 12),
new KeyValuePair<string, int>("AAA", 32),
new KeyValuePair<string, int>("AAB", 4),
new KeyValuePair<string, int>("AABB", 38),
new KeyValuePair<string, int>("BBAA", 3),
new KeyValuePair<string, int>("CDDDA", 76),
};
foreach (var p in FilterAndSort(list, "AA", list.Max(p => p.Key.Length)))
{
Console.WriteLine("{0} {1}", p.Key, p.Value);
}
AAA 32
AA 12
AABB 38
AAB 4