sorteddictionary

First item in a SortedDictionary?

℡╲_俬逩灬. 提交于 2019-12-02 03:43:23
There are many, many threads on ways to get the "first" item from a Dictionary , and various answers as to why such a thing is not really a good idea because there's no internal ordering. But mine's a SortedDictionary , so those arguments don't apply. Yet I cannot find a way to get the Nth item from a SortedDictionary any easier than a Dictionary . Here is my SD: FRs As SortedDictionary(Of DateTime, ScheduleItem) I see a few hints that I should be able to do: If FRs.Count = 1 Then FirstFR = FRs.Keys(0) But that is not valid in my code - it says it has no defaults and cannot be indexed. .First

Thread safe SortedDictionary

╄→尐↘猪︶ㄣ 提交于 2019-12-01 17:57:26
问题 I made a class that uses a SortedDictionary to store and manipulate data. The class works great except when it is implemented in a multi-threaded environment. Now, I would like to make the class thread safe by writing a wrapper class for the internal SortedDictionary class. I would like to use the Reader-Writer Locks to implement this but for now, I'm having problems just writing the wrapper class itself. Specifically, I'm not sure how to implement the Enumerator for the dictionary. Here is

Thread safe SortedDictionary

落花浮王杯 提交于 2019-12-01 17:39:59
I made a class that uses a SortedDictionary to store and manipulate data. The class works great except when it is implemented in a multi-threaded environment. Now, I would like to make the class thread safe by writing a wrapper class for the internal SortedDictionary class. I would like to use the Reader-Writer Locks to implement this but for now, I'm having problems just writing the wrapper class itself. Specifically, I'm not sure how to implement the Enumerator for the dictionary. Here is my complete code for the class as it stands now. public class ConcurrentSortedDictionary<TKey, TValue> :

Finding nearest value in a SortedDictionary

那年仲夏 提交于 2019-12-01 03:57:01
问题 I have a SortedDictionary SortedDictionary<int, CPUOptimizationObject> myDict; Now I want to find the first value above X. I can do something like this foreach (var iKey in MyDict.Keys) { if (iKey >= thresholdKey) { foundKey = iKey; break; } } but this isn't good performance wise. Any better suggestion? (is there a method for that in the collections something like Binary search for SortedDictionary ?) 回答1: While, in theory, finding the smallest item that is larger than a given value is an

How to use custom IComparer for SortedDictionary?

三世轮回 提交于 2019-12-01 03:10:12
I am having difficulties to use my custom IComparer for my SortedDictionary<>. The goal is to put email addresses in a specific format (firstnam.lastname@domain.com) as the key, and sort by last name. When I do something like this: public class Program { public static void Main(string[] args) { SortedDictionary<string, string> list = new SortedDictionary<string, string>(new SortEmailComparer()); list.Add("a.johansson@domain.com", "value1"); list.Add("b.johansson@domain.com", "value2"); foreach (KeyValuePair<string, string> kvp in list) { Console.WriteLine(kvp.Key); } Console.ReadLine(); } }

convert a dict to sorted dict in python

▼魔方 西西 提交于 2019-11-30 19:08:48
I want to convert a dict into sorted dict in python data = pandas.read_csv('D:\myfile.csv') for colname, dtype in data.dtypes.to_dict().iteritems(): if dtype == 'object': print colname count = data[colname].value_counts() d = dict((str(k), int(v)) for k, v in count.iteritems()) f = dict(sorted(d.iteritems(), key=lambda item: item[1], reverse = True)[:5]) print f m ={} m["count"]= int(sum(count)) m["Top 5"]= f print m k = json.dumps(m) print k f = {'Gears of war 3': 6, 'Batman': 5, 'gears of war 3': 4, 'Rocksmith': 5, 'Madden': 3} My desired Output is : f = {'Gears of war 3': 6, 'Batman': 5,

How do I get previous key from SortedDictionary?

只谈情不闲聊 提交于 2019-11-30 08:54:27
问题 I have dictionary containing key value pairs. SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>(); dictionary.Add(1,33); dictionary.Add(2,20); dictionary.Add(4,35); I want to get previous key value pair from a known key value. In the above case, if I have key 4, then how can I get <2,20> ? 回答1: It's hard to implement this efficiently with a SortedDictionary<TKey, TValue> since it is implemented as a binary search tree that does not expose predecessors or successors. You could

convert a dict to sorted dict in python

[亡魂溺海] 提交于 2019-11-30 03:38:40
问题 I want to convert a dict into sorted dict in python data = pandas.read_csv('D:\myfile.csv') for colname, dtype in data.dtypes.to_dict().iteritems(): if dtype == 'object': print colname count = data[colname].value_counts() d = dict((str(k), int(v)) for k, v in count.iteritems()) f = dict(sorted(d.iteritems(), key=lambda item: item[1], reverse = True)[:5]) print f m ={} m["count"]= int(sum(count)) m["Top 5"]= f print m k = json.dumps(m) print k f = {'Gears of war 3': 6, 'Batman': 5, 'gears of

How do I get previous key from SortedDictionary?

自闭症网瘾萝莉.ら 提交于 2019-11-29 09:16:14
I have dictionary containing key value pairs. SortedDictionary<int,int> dictionary=new SortedDictionary<int,int>(); dictionary.Add(1,33); dictionary.Add(2,20); dictionary.Add(4,35); I want to get previous key value pair from a known key value. In the above case, if I have key 4, then how can I get <2,20> ? It's hard to implement this efficiently with a SortedDictionary<TKey, TValue> since it is implemented as a binary search tree that does not expose predecessors or successors. You could of course just enumerate each KeyValuePair until you find the "known" key. With a little bit of LINQ, this

Python 2.6 TreeMap/SortedDictionary?

蓝咒 提交于 2019-11-29 05:40:17
Is there a built-in sorted dictionary implementation in Python 2.6, or are hashtables the only kind? Clarifications: I'm asking about sorted dictionarys, not ordered dictionaries! James Hurford I think the answer here is no. There is a Treemap but it isn't in the python standard library. http://pypi.python.org/pypi/treemap/ I think to those who don't like my answer, may think it's wrong due to a recent update. Please note this is for Python 2.6, not 2.7 or Python 3 Please add the correct answer if you still think it's wrong, or unhelpful, or at least give a reason why you think it's a bad