vb.net 如何将值插入(字符串,列表(字符串))字典的列表部分?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14266965/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 11:51:42  来源:igfitidea点击:

How can I insert values into the list part of a dictionary of(string, list(of string))?

vb.netlistdictionaryvisual-studio-2012

提问by Mellonjollie

I'm working with a dictionary(of string, list(of string))in VB to store some text data pulled from large .csv files.

我正在使用dictionary(of string, list(of string))VB 来存储一些从大型 .csv 文件中提取的文本数据。

The dictionary.key is just a date string like "2012-12-12", and the corresponding list value contains all of the files that are associated with that date.

dictionary.key 只是一个日期字符串,如“2012-12-12”,相应的列表值包含与该日期关联的所有文件。

Using a StreamReaderand a line.split, I can pull out the date string and compare it to the dictionary. If the dictionary does not contain that date, I want to insert it as a new key, then add the filename to the list for that key. This will loop through the whole file, then continue through every file the user has selected, using the same dictionary. Eventually I should see 4 or 5 file names per date, with no duplicate file names per date allowed.

使用 aStreamReader和 a line.split,我可以提取日期字符串并将其与字典进行比较。如果字典不包含该日期,我想将其作为新键插入,然后将文件名添加到该键的列表中。这将遍历整个文件,然后使用相同的字典继续浏览用户选择的每个文件。最终我应该看到每个日期有 4 或 5 个文件名,每个日期不允许有重复的文件名。

What is the correct way to use dictionary.addmethod to do this?

使用dictionary.add方法执行此操作的正确方法是什么?

回答by Mike Corcoran

something like this?

像这样的东西?

Dim key as String = "asdf"
If Not dict.ContainsKey(key) Then
    dict.Add(key, New List(Of String)(New String() {"1", "2", "3"}))
    Dim values = dict(key)
    If not values.Contains("some value") Then
        values.Add("some value")
    End If
End If

or something like this:

或类似的东西:

Dim key as String = "asdf"
If Not dict.ContainsKey(key) Then
    dict.Add("asdf", New List(Of String))
    dict(key).Add("1")
    dict(key).Add("2")
    ... and however many more values here ...
End If