相关疑难解决方法(0)

Dictionary中元素的顺序

我的问题是关于枚举字典元素

// Dictionary definition
private Dictionary<string, string> _Dictionary = new Dictionary<string, string>();

// add values using add

_Dictionary.Add("orange", "1");
_Dictionary.Add("apple", "4");
_Dictionary.Add("cucumber", "6");

// add values using []

_Dictionary["banana"] = 7;
_Dictionary["pineapple"] = 7;

// Now lets see how elements are returned by IEnumerator
foreach (KeyValuePair<string, string> kvp in _Dictionary)
{
  Trace.Write(String.Format("{0}={1}", kvp.Key, kvp.Value));
}
Run Code Online (Sandbox Code Playgroud)

枚举的元素将以什么顺序排列?我可以强制命令按字母顺序排列吗?

.net c# ienumerable dictionary

99
推荐指数
6
解决办法
8万
查看次数

C#中的双向1对1字典

我正在寻找C#(2)中的通用双向1到1字典类,即.a BiDictionaryOneToOne<T, S>保证只包含每个值和键中的一个(无论如何最多为RefEquals),并且可以使用键或值进行搜索.有人知道,或者我应该自己实施吗?我不敢相信我是第一个需要这个的人......

这个问题的答案中有一个BiDictionary ,但它不适用于唯一元素(并且也不实现RemoveByFirst(T t)或RemoveBySecond(S s)).

谢谢!

.net c# collections

98
推荐指数
3
解决办法
2万
查看次数

相对于键对字典进行排序

我在C#中有一本字典

Dictionary<Person, int>
Run Code Online (Sandbox Code Playgroud)

我想那种字典到位相对于键(在Person类的字段).我该怎么做?互联网上的每个可用帮助都是列表,没有特定的字典排序示例.任何帮助将非常感谢!

c# sorting dictionary

71
推荐指数
6
解决办法
9万
查看次数

将IOrderedEnumerable <KeyValuePair <string,int >>转换为Dictionary <string,int>

我正在关注另一个问题答案,我得到了:

// itemCounter is a Dictionary<string, int>, and I only want to keep
// key/value pairs with the top maxAllowed values
if (itemCounter.Count > maxAllowed) {
    IEnumerable<KeyValuePair<string, int>> sortedDict =
        from entry in itemCounter orderby entry.Value descending select entry;
    sortedDict = sortedDict.Take(maxAllowed);
    itemCounter = sortedDict.ToDictionary<string, int>(/* what do I do here? */);
}
Run Code Online (Sandbox Code Playgroud)

Visual Studio要求参数Func<string, int> keySelector.我尝试了一些我在网上找到并放入的半相关示例k => k.Key,但这会产生编译错误:

'System.Collections.Generic.IEnumerable<System.Collections.Generic.KeyValuePair<string,int>>' 不包含'ToDictionary'的定义,最好的扩展方法重载 'System.Linq.Enumerable.ToDictionary<TSource,TKey>(System.Collections.Generic.IEnumerable<TSource>, System.Func<TSource,TKey>)'有一些无效的参数

c# linq todictionary

37
推荐指数
2
解决办法
2万
查看次数

如何按键对字典进行排序

我有字典 Dictionary<string, Point>

键是c1,c3,c2,t1,t4,t2我想将它们排序为c1,c2,c3,t1,t2,t3

我正在尝试使用它进行排序

Input.OrderBy(key => key.Key );
Run Code Online (Sandbox Code Playgroud)

但它不起作用

任何想法如何解决这个问题

.net c# linq dictionary

28
推荐指数
5
解决办法
8万
查看次数

拖曳一个字符串,使两个相邻的字母不相同

我一直在努力解决这个面试问题,要求对一个字符串进行洗牌,以便没有两个相邻的字母是相同的.例如,

ABCC - > ACBC

我想到的方法是

1)迭代输入字符串并将(字母,频率)对存储在某个集合中

2)现在通过拉出我们不仅仅拉动的最高频率(即> 0)字母来构建结果字符串

3)每当我们拉信时更新(减少)频率

4)如果所有字母的频率为零,则返回结果字符串

5)如果我们只剩下一个频率大于1的字母,则返回错误

通过这种方法,我们可以为最后一个保存更珍贵(不太频繁)的字母.但为了实现这一点,我们需要一个集合,让我们可以有效地查询密钥,同时有效地按值对其进行排序.喜欢的东西会工作,除了我们需要保持每个字母检索排序后的集合.

我假设是Unicode字符.

关于使用什么集合的任何想法?还是另一种方法?

c# string algorithm data-structures

13
推荐指数
1
解决办法
1613
查看次数

C#Array,如何使数组中的数据彼此不同?

C#Array,如何使数组中的数据彼此不同?例如

string[] a = {"a","b","a","c","b","b","c","a"}; 
Run Code Online (Sandbox Code Playgroud)

如何获得

string[]b = {"a","b","c"}
Run Code Online (Sandbox Code Playgroud)

c# arrays distinct-values

10
推荐指数
2
解决办法
2万
查看次数

基于键对字典进行排序

我需要根据键在VB.net中订购一个Dictionary.键和值都是字符串.字典没有.Sort().有没有办法做到这一点,而无需编写自己的排序算法?

vb.net sorting dictionary

9
推荐指数
2
解决办法
3万
查看次数

如何在字典中找到最小键

我声明字典如下:

private Dictionary<int, touchInformation> touchDictionary = new Dictionary<int, touchInformation>();
Run Code Online (Sandbox Code Playgroud)

我使用如下:

touchDictionary[touchID] = touchObject;

因此,touchDictionary将保留来自touchID的密钥.现在,我尝试使用字典找到最小密钥,但我不知道该怎么做.有什么建议吗?

请注意,C.Porawat

c# dictionary

8
推荐指数
1
解决办法
1万
查看次数

按值排序字典

我有一个字典:

{ "honda" : 4, "toyota": 7, "ford" : 3, "chevy": 10 }
Run Code Online (Sandbox Code Playgroud)

我想通过第二列aka(值)降序对其进行排序.

期望的输出:

"雪佛兰",10

"丰田",7

"本田",4

"福特",3

vb.net sorting dictionary

7
推荐指数
1
解决办法
9751
查看次数