将字符串数组转换为字符串字典的最优雅的方法

时间:2021-07-19 00:23:31

Is there a built-in function for converting a string array into a dictionary of strings or do you need to do a loop here?

是否有将字符串数组转换为字符串字典的内置函数,还是需要在这里做一个循环?

6 个解决方案

#1


65  

Assuming you're using .NET 3.5, you can turn any sequence (i.e. IEnumerable<T>) into a dictionary:

假设您正在使用。net 3.5,您可以将任何序列(即IEnumerable )转换为字典:

var dictionary = sequence.ToDictionary(item => item.Key,
                                       item => item.Value)

where Key and Value are the appropriate properties you want to act as the key and value. You can specify just one projection which is used for the key, if the item itself is the value you want.

其中键和值是您希望作为键和值的适当属性。如果项本身是您想要的值,您可以只指定用于键的一个投影。

So for example, if you wanted to map the upper case version of each string to the original, you could use:

例如,如果你想将每个字符串的大小写版本映射到原始版本,你可以使用:

var dictionary = strings.ToDictionary(x => x.ToUpper());

In your case, what do you want the keys and values to be?

在你的情况下,你想要的键和值是什么?

If you actually just want a set (which you can check to see if it contains a particular string, for example), you can use:

如果你只是想要一个集合(例如,你可以检查它是否包含一个特定的字符串),你可以使用:

var words = new HashSet<string>(listOfStrings);

#2


13  

You can use LINQ to do this, but the question that Andrew asks should be answered first (what are your keys and values):

你可以用LINQ来做这个,但是Andrew问的问题应该先回答(你的键和值是什么):

using System.Linq;

string[] myArray = new[] { "A", "B", "C" };
myArray.ToDictionary(key => key, value => value);

The result is a dictionary like this:

结果就有了这样一本字典:

A -> A
B -> B
C -> C

#3


4  

If you need a dictionary without values, you might need a HashSet:

如果您需要一个没有值的字典,您可能需要一个HashSet:

var hashset = new HashSet<string>(stringsArray);

#4


3  

What do you mean?

你是什么意思?

A dictionary is a hash, where keys map to values.

字典是一个散列,键映射到值。

What are your keys and what are your values?

你的钥匙是什么,你的价值是什么?

foreach(var entry in myStringArray)
    myDictionary.Add(????, entry);

#5


3  

IMO, When we say an Array we are talking about a list of values that we can get a value with calling its index (value => array[index]), So a correct dictionary is a dictionary with a key of index.

在我看来,当我们说一个数组时,我们说的是一个值列表,我们可以通过调用它的索引(value => Array [index])得到一个值,所以正确的字典就是一个索引键的字典。

And with thanks to @John Skeet, the proper way to achieve that is:

感谢@John Skeet,实现这个目标的正确方法是:

var dictionary = array
    .Select((v, i) => new {Key = i, Value = v})
    .ToDictionary(o => o.Key, o => o.Value);

Another way is to use an extension method like this:

另一种方法是使用这样的扩展方法:

public static Dictionary<int, T> ToDictionary<T>(this IEnumerable<T> array)
{
    return array
        .Select((v, i) => new {Key = i, Value = v})
        .ToDictionary(o => o.Key, o => o.Value);
}

#6


0  

            Dictionary<int, string> dictionaryTest = new Dictionary<int, string>();

            for (int i = 0; i < testArray.Length; i++)
            {
                dictionaryTest.Add(i, testArray[i]);
            }

            foreach (KeyValuePair<int, string> item in dictionaryTest)
            {


                Console.WriteLine("Array Position {0} and Position Value {1}",item.Key,item.Value.ToString()); 
            }

#1


65  

Assuming you're using .NET 3.5, you can turn any sequence (i.e. IEnumerable<T>) into a dictionary:

假设您正在使用。net 3.5,您可以将任何序列(即IEnumerable )转换为字典:

var dictionary = sequence.ToDictionary(item => item.Key,
                                       item => item.Value)

where Key and Value are the appropriate properties you want to act as the key and value. You can specify just one projection which is used for the key, if the item itself is the value you want.

其中键和值是您希望作为键和值的适当属性。如果项本身是您想要的值,您可以只指定用于键的一个投影。

So for example, if you wanted to map the upper case version of each string to the original, you could use:

例如,如果你想将每个字符串的大小写版本映射到原始版本,你可以使用:

var dictionary = strings.ToDictionary(x => x.ToUpper());

In your case, what do you want the keys and values to be?

在你的情况下,你想要的键和值是什么?

If you actually just want a set (which you can check to see if it contains a particular string, for example), you can use:

如果你只是想要一个集合(例如,你可以检查它是否包含一个特定的字符串),你可以使用:

var words = new HashSet<string>(listOfStrings);

#2


13  

You can use LINQ to do this, but the question that Andrew asks should be answered first (what are your keys and values):

你可以用LINQ来做这个,但是Andrew问的问题应该先回答(你的键和值是什么):

using System.Linq;

string[] myArray = new[] { "A", "B", "C" };
myArray.ToDictionary(key => key, value => value);

The result is a dictionary like this:

结果就有了这样一本字典:

A -> A
B -> B
C -> C

#3


4  

If you need a dictionary without values, you might need a HashSet:

如果您需要一个没有值的字典,您可能需要一个HashSet:

var hashset = new HashSet<string>(stringsArray);

#4


3  

What do you mean?

你是什么意思?

A dictionary is a hash, where keys map to values.

字典是一个散列,键映射到值。

What are your keys and what are your values?

你的钥匙是什么,你的价值是什么?

foreach(var entry in myStringArray)
    myDictionary.Add(????, entry);

#5


3  

IMO, When we say an Array we are talking about a list of values that we can get a value with calling its index (value => array[index]), So a correct dictionary is a dictionary with a key of index.

在我看来,当我们说一个数组时,我们说的是一个值列表,我们可以通过调用它的索引(value => Array [index])得到一个值,所以正确的字典就是一个索引键的字典。

And with thanks to @John Skeet, the proper way to achieve that is:

感谢@John Skeet,实现这个目标的正确方法是:

var dictionary = array
    .Select((v, i) => new {Key = i, Value = v})
    .ToDictionary(o => o.Key, o => o.Value);

Another way is to use an extension method like this:

另一种方法是使用这样的扩展方法:

public static Dictionary<int, T> ToDictionary<T>(this IEnumerable<T> array)
{
    return array
        .Select((v, i) => new {Key = i, Value = v})
        .ToDictionary(o => o.Key, o => o.Value);
}

#6


0  

            Dictionary<int, string> dictionaryTest = new Dictionary<int, string>();

            for (int i = 0; i < testArray.Length; i++)
            {
                dictionaryTest.Add(i, testArray[i]);
            }

            foreach (KeyValuePair<int, string> item in dictionaryTest)
            {


                Console.WriteLine("Array Position {0} and Position Value {1}",item.Key,item.Value.ToString()); 
            }