我看到了这个问题.
如何在.Net 3.5中获取SortedDictionary中的最后一个元素.
如果.NET有一个SortedDictionary对象......请问Java中的这个是什么?我还需要能够Enumeration在Java代码中检索(元素)..因此我可以迭代所有键.
我在想它是一个TreeMap?但是,我不认为有Enumeration这样的暴露?
有任何想法吗?
当我SortedDictionary<TK, TV>在.NET中,我想枚举它,ICollection<KeyValuePair<TK, TV>>它按预期顺序枚举?
那是KeyValuePair<TK, TV>用最低的键作为第一个返回,后面KeyValuePair<TK, TV>跟第二个最低的键等?
注意:只接受通过引用备份的答案.
我想在dthon中将dict转换为已排序的dict
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}
Run Code Online (Sandbox Code Playgroud)
我想要的输出是:
f = {'Gears of war 3': 6, 'Batman': 5, …Run Code Online (Sandbox Code Playgroud) 我想做一些非常简单的事情,但似乎我不明白SortedDictionary.
我要做的是以下内容:
创建一个排序字典,按一些浮点数对我的项目进行排序,因此我创建了一个类似于此的字典
SortedDictionary<float, Node<T>> allNodes = new SortedDictionary<float, Node<T>>();
Run Code Online (Sandbox Code Playgroud)
现在我添加项目后,我想逐个删除它们(每次删除都应该从最小到最大的复杂度为O(log(n)).
我该怎么做?我认为这只allNodes[0]会给我最小的,但事实并非如此.
更重要的是,似乎字典无法处理重复的键.我觉得我使用的是错误的数据结构......
如果我有一堆节点需要按照它们的距离(浮点)进行排序,我应该使用其他东西吗?
我是一个完整的LINQ新手,所以我不知道我的LINQ是否不正确我需要做什么或者我的性能期望是否太高.
我有一个对象的SortedList,用int键入; SortedList与SortedDictionary相对,因为我将使用预先排序的数据填充集合.我的任务是找到确切的密钥,或者,如果没有确切的密钥,则找到具有下一个更高值的密钥.如果列表的搜索太高(例如,最高键为100,但搜索105),则返回null.
// The structure of this class is unimportant. Just using
// it as an illustration.
public class CX
{
public int KEY;
public DateTime DT;
}
static CX getItem(int i, SortedList<int, CX> list)
{
var items =
(from kv in list
where kv.Key >= i
select kv.Key);
if (items.Any())
{
return list[items.Min()];
}
return null;
}
Run Code Online (Sandbox Code Playgroud)
给定50,000条记录的列表,调用getItem 500次需要大约一秒半.拨打50,000次电话需要2分钟.这种表现似乎很差.我的LINQ坏了吗?我期待太多了吗?我应该滚动自己的二元搜索功能吗?
我应该维护
1.)一个SortedDictionary(double,struct)
2.)或者只是一个普通的Dictionary(double,struct)加一个SortedSet(double)?
我只想要快速插入.我不关心检索,因为我不会做很多查找.我需要排序自然因为,我所做的唯一查找将是最大双倍或几个最大双倍.
我觉得时间表现明智 - 两者都是一样的,SortedSet<double>只做额外的工作.你们能证实吗?
我不知道的部分是维持排序,SortedDictionary仅仅是键(双打),还是键和值的移动.在后一种情况下2.)将胜过1.),不是吗?
此外,尚不清楚SortedDictionary内部如何实施.Sortedset是红黑树,是一个经过验证的表演者.
我需要一个像a一样的数据结构,SortedDictionary<int, double>但是根据值而不是键来排序.当我们在字典中有大约3000个项目时,我需要大约1-2微秒来添加和删除项目.
我的第一个想法是简单地在我的代码中切换键和值.这几乎是有效的.通过这样做,我可以在测试中添加和删除元素大约1.2微秒.
但是键必须在SortedDictionary中是唯一的,这意味着我的逆字典中的值必须是唯一的.在某些情况下,他们可能不会.
.NET库中的某些想法已经对我有用吗?
我使用排序字典来维护项目列表,我经常需要监视前x项的状态.每次我更新一个项目时,我都想快速找出我所指的项目使用的索引.我知道我可以列举整个列表并计算我的位置,但是我正在寻找O(log n)时间或更好的东西,所有排序的字典都在RedBlack树上.每个节点都应该能够跟踪其子节点,这应该是一个快速计算.
我创建了一个使用a SortedDictionary来存储和操作数据的类.除非在多线程环境中实现,否则该类很有效.现在,我想通过为内部SortedDictionary类编写包装类来使类线程安全.我想使用Reader-Writer Locks来实现它,但是现在,我在编写包装器本身时遇到了问题.具体来说,我不知道如何实现Enumerator字典.这是我现在所用的完整代码.
public class ConcurrentSortedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>
{
#region Variables
SortedDictionary<TKey, TValue> _dict;
#endregion
#region Constructors
public ConcurrentSortedDictionary()
{
_dict = new SortedDictionary<TKey, TValue>();
}
public ConcurrentSortedDictionary(IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(comparer);
}
public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary);
}
public ConcurrentSortedDictionary(IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
{
_dict = new SortedDictionary<TKey, TValue>(dictionary, comparer);
}
#endregion
#region Properties
public IComparer<TKey> Comparer
{
get
{
return _dict.Comparer;
} …Run Code Online (Sandbox Code Playgroud) sorteddictionary ×10
c# ×7
.net ×4
dictionary ×3
collections ×2
enumeration ×1
idictionary ×1
ienumerable ×1
java ×1
linq ×1
pandas ×1
performance ×1
python ×1
sorted ×1
sortedlist ×1
sortedset ×1
wrapper ×1