劈裂串入的话基于长度的名单C# [英] Spliting string into words length-based lists c#
问题描述
我有空格隔开的单词的字符串。如何将字符串分割成基础上,单词长度一大堆单词?
示例
输入:
AA AAA AAAA BB BBB BBBB CC CCC CCCC CCCC BBB BB AA
输出:
列表1 = {AA,BB,CC}
名单2 = {AAA,BBB,CCC}
名单3 = {AAAA,BBBB,CCCC}
块引用>
解决方案
编辑:我很高兴我原来的答复帮助OP解决他们的问题。然而,琢磨这个问题有点后,我已经适应它(我强烈建议的对的我以前的解决方案,我在帖子的末尾留下)。
块引用>
一个简单的方法
字符串输入=AA AAA AAAA BB BBB BBBB CC CCC CCCC CCCC BBB BB AA;
VAR字= input.Trim()斯普利特()是不同的()。;
VAR查找= words.ToLookup(字=> word.Length);
说明
首先,我们修剪输入,以避免从外空间空元素。然后,我们分割字符串到一个数组。如果在字与字之间出现多个空格的 的,你需要使用
StringSplitOptions
为在的标记的回答。
调用
鲜明
只包括一次每个词,我们现在转换字<后/ $ C $从
的IEnumerable< C>;字符串>
到查找< INT,串>
,其中写着长通过密钥(INT)
和他们自己都存储在值字表示(串)
。
死守,这怎么可能?难道我们没有为每个键多个单词?当然,不过这也正是
查找
类是有:
查找< TKEY的,TElement>
强>表示每个映射到一个或多个
值的键的集合。
A查找< TKEY的,TElement>
类似于词典< TKEY的,TValue>
。所不同的是
A字典映射键单个值,而一个Lookup
键映射到值的集合的。
您可以通过调用
ToLookup $ C $创建一个
查找
的一个实例C>上的一个对象
实现的IEnumerable< T>
注意结果
没有公共构造函数来创建一个查询的新实例。
此外,查找对象是不可改变的,也就是说,你不能添加或
从已创建后,查找元素或键。
块引用>
字=> word.Length
是的KeySelectors 的拉姆达:它定义了我们想要索引(或组,如果你愿意)的查找
通过单词的长度。
用法
写所有的话到控制台
的(类似问题的最初请求输出)的
的foreach(在查找VAR分组)
{
Console.WriteLine({0}:{1},grouping.Key,的string.join(,分组));
}
输出
2:AA,BB,CC
3:AAA,BBB,CCC
4:AAAA,BBBB ,中交
块引用>
将一定长度的所有单词在
列表
列表<串GT;项目list3 =查找[3] .ToList();
按键
订单
的 (注意,这些将返回
IOrderedEnumerable< T>
,因此通过密钥访问已不再可能)的VAR orderedAscending = lookup.OrderBy(分组=> grouping.Key);
VAR orderedDescending = lookup.OrderByDescending(分组=> grouping.Key);
原来的答复 - 请的不做到这一点的(糟糕的表现,代码杂波):
字符串输入=AA AAA AAAA BB BBB BBBB CC CCC CCCC CCCC BBB BB AA;
&字典LT; INT,字符串[]>结果=新词典< INT,字符串[]>();
无功分组= input.Trim()斯普利特()是不同的()GROUPBY(S = GT; s.Length)。
.OrderBy(G => g.Key); //或:OrderByDescending(G => g.Key);
的foreach(在分组VAR分组)
{
results.Add(grouping.Key,grouping.ToArray());
}
I have a string of words separated by spaces. How to split the string into lists of words based on the words length?
Example
input:
" aa aaa aaaa bb bbb bbbb cc ccc cccc cccc bbb bb aa "
output :
List 1 = { aa, bb, cc} List 2 = { aaa, bbb, ccc} List 3 = { aaaa, bbbb, cccc}
解决方案Edit: I'm glad my original answer helped the OP solve their problem. However, after pondering the problem a bit, I've adapted it (and I strongly advise against my former solution, which I have left at the end of the post).
A simple approach
string input = " aa aaa aaaa bb bbb bbbb cc ccc cccc cccc bbb bb aa "; var words = input.Trim().Split().Distinct(); var lookup = words.ToLookup(word => word.Length);
Explanation
First, we trim the input to avoid empty elements from the outer spaces. Then, we split the string into an array. If multiple spaces occur in between the words, you'd need to use
StringSplitOptions
as as in Mark's answer.After calling
Distinct
to only include each word once, we now convertwords
fromIEnumerable<string>
toLookup<int, string>
, where the words' length is represented by the key(int)
and the words themselves are stored in the value(string)
.Hang on, how is that even possible? Don't we have multiple words for each key? Sure, but that's exactly what the
Lookup
class is there for:
Lookup<TKey, TElement>
represents a collection of keys each mapped to one or more values. ALookup<TKey, TElement>
resembles aDictionary<TKey, TValue>
. The difference is that a Dictionary maps keys to single values, whereas a Lookup maps keys to collections of values.You can create an instance of a
Lookup
by callingToLookup
on an object that implementsIEnumerable<T>
.
Note
There is no public constructor to create a new instance of a Lookup. Additionally, Lookup objects are immutable, that is, you cannot add or remove elements or keys from a Lookup after it has been created.
word => word.Length
is the KeySelector lambda: it defines that we want to index (or group, if you will) theLookup
by the Length of the words.Usage
Write all the words to the console
(similar to the question's originally requested output)
foreach (var grouping in lookup) { Console.WriteLine("{0}: {1}", grouping.Key, string.Join(", ", grouping)); }
Output
2: aa, bb, cc 3: aaa, bbb, ccc 4: aaaa, bbbb, cccc
Put all words of a certain length in a
List
List<String> list3 = lookup[3].ToList();
Order by key
(note that these will return
IOrderedEnumerable<T>
, so access by key is no longer possible)var orderedAscending = lookup.OrderBy(grouping => grouping.Key); var orderedDescending = lookup.OrderByDescending(grouping => grouping.Key);
Original answer - please don't do this (bad performance, code clutter):
string input = " aa aaa aaaa bb bbb bbbb cc ccc cccc cccc bbb bb aa "; Dictionary<int, string[]> results = new Dictionary<int, string[]>(); var grouped = input.Trim().Split().Distinct().GroupBy(s => s.Length) .OrderBy(g => g.Key); // or: OrderByDescending(g => g.Key); foreach (var grouping in grouped) { results.Add(grouping.Key, grouping.ToArray()); }
这篇关于劈裂串入的话基于长度的名单C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!