C# - 处理字典中的值
这可能是一个简单的问题,但我无法找出一个简单的方法来做到这一点。我目前有一个带有 KeyValuePairs 的简单字典,如下所示:
<1, 100> <2, 200> <3,-999> <4, 134> <5,-999> <6, 223> <7、123>
该值 (-999) 指示发生的错误。我想做的是迭代字典,每当出现 -999 时,就使用以下键的值。根据上面的例子,结果将是:
<1, 100> <2, 200> <3, 134> <4, 134> <5, 223> <6, 223> <7、123>
任何想法将不胜感激!
This may be a simple question, but I can't figure out a simple way of doing this. I currently have a simple dictionary with KeyValuePairs like the following:
<1, 100> <2, 200> <3, -999> <4, 134>
<5, -999> <6, 223> <7, 123>
The value's (-999) are an indicator for an error that occurs. What I am trying to do is iterate through the dictionary and for whenever there occurs a -999 use the value for the following key. The result, given the above example, would be:
<1, 100> <2, 200> <3, 134> <4, 134>
<5, 223> <6, 223> <7, 123>
Any thoughts would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要假设您的键有一个顺序(这是必要的,因为否则字典没有顺序)。您可能需要考虑使用
SortedDictionary
。无论哪种方式,您都可以说:
请注意,您没有指定如果“最后”项是 -999 该怎么办。目前它保持在-999。
You need to assume that your keys have an ordering (this is necessary because dictionaries otherwise don't have an ordering). You might want to consider using a
SortedDictionary
.Either way, you can say:
Note that you didn't specify what to do if the "last" item is -999. For now it remains at -999.
添加第 n 个元素时是否可以访问第 n+1 个元素?如果是这样,您可以在添加第 n 个键时替换第 n+1 个值。
如果没有,那么您需要一个有序的集合。我不确定 SortedDictionary 是否是您所需要的,因为键可能不是您的排序顺序?同样令人困惑的是,您列出了几个具有相同密钥的 K,V 对。字典不会喜欢这样的。
钥匙在V里面吗?就像 ID 属性一样?如果是这样,您可以尝试从 KeyedCollection 派生一个类,该类保留您添加项目的顺序,并让您决定值的哪个属性是键(如果它适合您)。但同样,没有重复的键。
Do you have access to the n+1-th element when you're adding the nth element? If so, you could substitute n+1-th value when you add nth key.
If not then you need an ordered collection. I'm not sure SortedDictionary would be what you need becuase the keys might not be your sort order? It is also puzzling that you list several K,V pairs with identical keys. Dictionaries won't like that.
Is the key within the V? Like an ID property? If so, You could try to derive a class from KeyedCollection which preserves the order in which you added items and lets you decide what property of the value is the key if it works that way for you. But again, no duplicate keys.
我假设您想要按照添加到字典中的顺序处理值,而不是按照它们可能排序的顺序。我认为您最好使用 Stack<> 。通过这种方式,您可以将所有 KeyValuePair 压入堆栈,然后将它们向后弹出。当您找到 -999 时,您可以应用从堆栈中弹出的上一个 KeyValuePair 的值并继续。
I'm going to assume that you want to process the values in the order they are added to the dictionary, and not the order they might happen to sort in. I think you might be better off using a Stack<>. This way you can push all the KeyValuePairs onto the stack, then pop them off backwards. When you find a -999 you can apply the value of the previous KeyValuePair that was popped off the stack and move on.
如果您不需要它们在字典中,您可以使用链接列表,然后您可以从末尾开始并循环遍历列表以更新 -999 值。
If you don't need them in a dictionary you could use a linked list, then you could start at the end and loop through the list to update the -999 values.